Class: @property -> .__dict__

Peter Otten __peter__ at web.de
Fri Dec 16 04:32:45 EST 2011


Ulrich wrote:

> if I replace it to
> def attributelist(self):
>          # find all attributes to the class that are of type numpy
> arrays:
>          return [attr for attr in dir(self) if
> isinstance(getattr(self, attr), numpy.ndarray)]
> 
> it crashes going into some kind of endless loop.
> 
> Do you happen to have any idea?

dir(self) finds an attribute named "attributelist", getattr(self, 
"attributelist") then tries to calculate the value of that attribute, 
invokes dir(self) which finds an attribute named "attributelist" and so on 
ad infinitum or the stack overflows. Try (untested)

@property
def attributelist(self):
    return [attr for attr in dir(self) if attr != "attributelist" and
            isinstance(getattr(self, attr), numpy.ndarray)]

to avoid the infinite recursion.




More information about the Python-list mailing list