This is code I have, but it looks like non-python.
def __contains__(self, childName):
"""Determines if item is a child of this item"""
for c in self.children:
if c.name == childName:
return True
return False
What is the most “python” way of doing this? Use a lambda filter function? For some reason very few examples online actually work with a list of objects where you compare properties, they always show how to do this using a list of actual strings, but that’s not very realistic.
I would use:
This is short, and has the same advantage as your code, that it will stop when it finds the first match.
If you’ll be doing this often, and speed is a concern, you can create a new attribute which is the set of child names, and then just use
return childName in self.childNames, but then you have to update the methods that change children to keep childNames current.