Assuming that your object has only a title attribute which is relevant for equality, you have to implement the __eq__ method as follows:
class YourObject:
[...]
def __eq__(self, other):
return self.title == other.title
Of course if you have more attributes that are relevant for equality, you must include those as well. You might also consider implementing __ne__ and __cmp__ for consistent behaviour.
Assuming that your object has only a title attribute which is relevant for equality, you have to implement the __eq__ method as follows:
class YourObject:
[...]
def __eq__(self, other):
return self.title == other.title
Of course if you have more attributes that are relevant for equality, you must include those as well. You might also consider implementing __ne__ and __cmp__ for consistent behaviour.
In case the objects are not the same instance, you need to implement the __eq__ method for python to be able to tell when 2 objects are actually equal.
Of course that most library types, such as strings and lists already have __eq__ implemented, which may be the reason comparing titles works for you (are they strings?).
For further information see the python documentation.
Here is a random example for __eq__.
Python, how to check object is in list? - Stack Overflow
python check if an object is in a list of objects - Stack Overflow
Can I check if an item in a list is a specific type of object?
how to quickly check if an object is in a list in python - Stack Overflow
Videos
your example works fine, the only problem you could have is a performance issue with the in operator for list which is O(n).
If you don't care about the order of the keys (which is likely), just do this:
self.key_vertices = set()
then:
if key not in self.key_vertices:
# add a new key to the array
self.key_vertices.add(key)
# create a vertex object to store dependency information
self.verteces.append(Vertex(key, children))
you'll save a lot of time in the in operator because set in is way faster due to key hashing.
And if you don't care about order in self.verteces, just do a dictionary, and in that case, you probably don't need the first key parameter to your Vertex structure.
self.verteces = dict()
if key not in self.verteces:
# create a vertex object to store dependency information
self.verteces[key] = Vertex(children)
When you need to check for membership, a list is not the best choice as every object in the list will be checked.
If key is hashable, use a set.
If it's not hashable but is comparable, use a tree (unavailable in the standard library). Try to make it hashable.
As you can easily see from the documentation, the any() function short-circuits an returns True as soon as a match has been found.
any(x.name == "t2" for x in l)
Another built-in function next() can be used for this job. It stops at the first instance where the condition is True much like any().
next((True for x in l if x.name=='t2'), False)
Also, next() can return the object itself where the condition is True (so behaves like first() function in the OP).
next((x for x in l if x.name == 't2'), None)