Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 17011

How can I check if an instance of a class exists in a List in Python 3 according to value semantics instead of object identity?

$
0
0

In Python 3, I have a list (my_array) that contains an instance of the Demo class with a certain attribute set on that instance. In my case, the attribute is value: int = 4.

Given an instance of the Demo class, how can I determine if that instance already exists in my_array with the same properties. Here's a MCRE of my issue:

from typing import Listclass Demo:    def __init__(self, value: int):        self.value = valuemy_array: List[Demo] = [Demo(4)]print(Demo(4) in my_array)  # prints False

However, Demo(4) clearly already exists in the list, despite that it prints False.

I looked through the Similar Questions, but they were unrelated.

How can I check if that already exists in the list?


Viewing all articles
Browse latest Browse all 17011

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>