开发者

Python's getattr gets called twice?

开发者 https://www.devze.com 2023-01-14 16:56 出处:网络
I am using this simple example to understand Python\'s getatt开发者_Go百科r function: In [25]: class Foo:

I am using this simple example to understand Python's getatt开发者_Go百科r 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.


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


You're also using IPython. The stock CPython REPL doesn't exhibit this behavior.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号