Python的getattr被调用两次?

the*_*man 3 python reflection python-2.6 getattr

我使用这个简单的例子来理解Python的getattr函数:

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

In [26]: f = Foo()

In [27]: f.bar
bar
bar
Run Code Online (Sandbox Code Playgroud)

为什么bar打印两次?使用Python 2.6.5.

Cod*_*ict 9

我认为这是由于IPython.

要"修复"它,你必须禁用自动调用: %autocall 0

这是%autocall不可避免的副作用:因为它必须分析命令行中的对象以查看它是否可调用,python会触发对它的getattr调用.

资料来源:http://mail.scipy.org/pipermail/ipython-user/2008-June/005562.html