[IPython-dev] Strange property bug?

Paul Mueller gakusei at dakotacom.net
Sat May 19 18:10:40 EDT 2007

On Fri, May 18, 2007 at 07:52:51PM -0700, Anand Patil wrote:
> Hi all,
> Here's a script I called 'test.py':
> ---------------------------------
> class A(object):
>     c=1
>     def fget(self):
>         self.c=self.c+1
>         print 'Adding 1 to c'
>     b=property(fget)
> M = A()
> ----------------------
>  From the regular Python shell, the A class works as expected:


> But in IPython, the fget function seems to get called twice:


> Any ideas?

This is a result of autocall. Trying it with autocall on and off yields:

In [7]:%autocall
Automatic calling is: Smart

In [8]:M.b
Adding 1 to c
Adding 1 to c

In [9]:%autocall
Automatic calling is: OFF

In [10]:M.b
Adding 1 to c

If you want more info:

More specifically, the following line in ipython (in
IPython/prefilter.py's function checkAutocall) is indirectly
responsible, and mentions the problem:

oinfo = l_info.ofind(ip) # This can mutate state via getattr

It is only executed if autocall is on.

If autocall is on, ipython eventually calls getattr(M, 'b') as part of
determining if M.b should be autocalled, then later actually executes
M.b, so fget() ends up getting called twice.

Paul Mueller

More information about the IPython-dev mailing list