Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #17361

Re: Class: @property -> .__dict__

References <ad04c206-dbfd-4e49-b365-943b47844c41@n6g2000vbg.googlegroups.com> <4eeb0949$0$29979$c3e8da3$5496439d@news.astraweb.com> <3fe1021b-75ae-4e3d-a5b0-175ad00aefa4@i6g2000vbh.googlegroups.com> <08b0a976-dd2a-4728-b865-4d04f4e5b7fb@n10g2000vbg.googlegroups.com> <jcf37p$dt5$1@dough.gmane.org>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2011-12-16 09:23 -0700
Subject Re: Class: @property -> .__dict__
Newsgroups comp.lang.python
Message-ID <mailman.3736.1324052667.27778.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Class: @property -> .__dict__ Ulrich <ulrich@dorda.net> - 2011-12-16 00:52 -0800
  Re: Class: @property -> .__dict__ Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-16 09:03 +0000
    Re: Class: @property -> .__dict__ Ulrich <ulrich.dorda@gmail.com> - 2011-12-16 01:11 -0800
      Re: Class: @property -> .__dict__ Ulrich <ulrich.dorda@gmail.com> - 2011-12-16 01:14 -0800
        Re: Class: @property -> .__dict__ Peter Otten <__peter__@web.de> - 2011-12-16 10:32 +0100
        Re: Class: @property -> .__dict__ Ian Kelly <ian.g.kelly@gmail.com> - 2011-12-16 09:23 -0700
  Re: Class: @property -> .__dict__ Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2011-12-16 11:02 +0100

csiph-web