Strange behavior of __get__ in a descriptor in IPython

Diez B. Roggisch deets at nospam.web.de
Wed Nov 7 11:11:05 CET 2007


Jakub Hegenbart wrote:

> Hi,
> 
> I'm studying the descriptor protocol and its usage from the following
> document:
> 
> http://users.rcn.com/python/download/Descriptor.htm
> 
> There is some sample code:
> 
> http://users.rcn.com/python/download/Descriptor.htm#descriptor-example
> 
> that behaves in a different way on my machine than the example suggests:
> 
> In [2]: a=MyClass()
> 
> In [3]: a.x
> Retrieving var "x"
> Retrieving var "x"
> Out[3]: 1
> 
> On the other hand, in the 'plain' Python shell, it's invoked only once as
> expected:
> 
>>>> a=desc.MyClass()
>>>> a.x
> Retrieving var "x"
> 10
>>>>
> 
> Should I take as granted that IPython might in some cases access an
> attribute
> of an object more than once even in face of side effects, or is this a
> bug?

I'm not sure, but you could try and set up a counter that counts the number
of accesses.

But to be honest: I don't think it's a bug - this is one hell of an
essential part of python, not working here would cause all kinds of
troubles.

Instead, I presume the printing/shell-part of IPython is responsible.

Diez



More information about the Python-list mailing list