[IPython-user] strange repeated call to getattr

Fernando Perez fperez.net@gmail....
Fri Jun 20 13:42:10 CDT 2008


On Fri, Jun 20, 2008 at 8:12 AM, John Hunter <jdh2358@gmail.com> wrote:

> On line 3 getattr is called twice.  Is this expected?

By default, yes:

In [1]: run getattr_test.py
1 attr
None

In [2]: foo.jdh
2 attr
3 attr

But if  you disable autocall:

In [3]: %autocall  0
Automatic calling is: OFF

In [4]: run getattr_test.py
1 attr
None

In [5]: foo.jdh
2 attr


it doesn't happen.  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.  Note that readline
does the same thing, just hit 'foo.<TAB>' in your example:

In [2]: foo.2<TAB HERE> attr
3 attr
4 attr
5 attr
6 attr
7 attr
count
Out[2]: 7


Lesson: getattr with side effects is bad.  As a general rule, do not
wire nuclear ballistic missile triggers as side effects of  your
getattr calls, lest a simple tab-completions starts Armageddon...

Cheers,

f


More information about the IPython-user mailing list