Using 'in' to match an attribute of Python objects in an array

后端 未结 8 1524
傲寒
傲寒 2021-01-07 16:25

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 ob         


        
8条回答
  •  -上瘾入骨i
    2021-01-07 16:36

    Are you looking to get a list of objects that have a certain attribute? If so, a list comprehension is the right way to do this.

    result = [obj for obj in listOfObjs if hasattr(obj, 'attributeName')]
    

提交回复
热议问题