Strange behavior of __get__ in a descriptor in IPython

  • Thread starter Jakub Hegenbart
  • Start date
J

Jakub Hegenbart

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

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?

Regards,

Jakub Hegenbart
 
D

Diez B. Roggisch

Jakub said:
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

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
 
F

Fernando Perez

Jakub said:
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

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?

Yup, IPython does access attributes more than once in an attempt to determine
if things can be called as functions. This behavior, however, only exists
if 'autocall' is active. Here's an example:

In [1]: run desc

In [2]: m.x
Retrieving var "x"
Retrieving var "x"
Out[2]: 10

In [3]: m.x
Retrieving var "x"
Retrieving var "x"
Out[3]: 10

In [4]: autocall 0
Automatic calling is: OFF

In [5]: m.x
Retrieving var "x"
Out[5]: 10


As you can see, once autocall is disabled, the double access goes away. There
really is no way to provide the autocall feature without any side effects
whatsoever, so if you need to avoid them at all costs, disable this feature.
You can do it permanently by editing your ipythonrc file.

Cheers,

f
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,691
Members
48,796
Latest member
Greg L.

Latest Threads

Top