I don’t remember whether I was dreaming or not but I seem to recall there being a function which allowed something like,
foo in iter_attr(array of python objects, attribute name)
I’ve looked over the docs but this kind of thing doesn’t fall under any obvious listed headers
Using a list comprehension would build a temporary list, which could eat all your memory if the sequence being searched is large. Even if the sequence is not large, building the list means iterating over the whole of the sequence before
incould start its search.The temporary list can be avoiding by using a generator expression:
Now, as long as
obj.id == 12near the start ofbar, the search will be fast, even ifbaris infinitely long.As @Matt suggested, it’s a good idea to use
hasattrif any of the objects inbarcan be missing anidattribute: