Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python's getattr gets called twice?

I am using this simple example to understand Python's getattr function:

In [25]: class Foo:
   ....:     def __getattr__(self, name):
   ....:         print name
   ....:         
   ....:         

In [26]: f = Foo()

In [27]: f.bar
bar
bar

Why is bar printed twice? Using Python 2.6.5.

like image 823
thebossman Avatar asked Oct 23 '25 16:10

thebossman


1 Answers

I think it's due to IPython.

To "fix" it, you have to disable autocall: %autocall 0

It's an inevitable side-effect of %autocall: since it has to analyze the object in the command line to see if it's callable, python triggers getattr calls on it.

Source: http://mail.scipy.org/pipermail/ipython-user/2008-June/005562.html

like image 176
Coding District Avatar answered Oct 25 '25 06:10

Coding District