Class: @property -> .__dict__

Ian Kelly ian.g.kelly at gmail.com
Fri Dec 16 11:23:47 EST 2011


On Fri, Dec 16, 2011 at 2:32 AM, Peter Otten <__peter__ at web.de> wrote:
> 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.

Or remove attributelist from the class (it feels more like a generic
function than a class property to me) or make it a method instead of a
property.



More information about the Python-list mailing list