我使用这个简单的例子来了解Python的GETATTR功能:Python的getattr被调用两次?
In [25]: class Foo:
....: def __getattr__(self, name):
....: print name
....:
....:
In [26]: f = Foo()
In [27]: f.bar
bar
bar
为什么bar
打印两次?使用Python 2.6.5。
我使用这个简单的例子来了解Python的GETATTR功能:Python的getattr被调用两次?
In [25]: class Foo:
....: def __getattr__(self, name):
....: print name
....:
....:
In [26]: f = Foo()
In [27]: f.bar
bar
bar
为什么bar
打印两次?使用Python 2.6.5。
我认为这是由于IPython。
“修理”它,你必须禁用自动调用:%autocall 0
这是 %自动调用不可避免的副作用:因为它必须分析在命令行 对象,看是否 它是可调用的,python触发getattr 调用它。
来源:http://mail.scipy.org/pipermail/ipython-user/2008-June/005562.html
感谢您的详细信息! – thebossman 2010-09-05 01:20:55
您还在使用IPython。库存CPython REPL不显示此行为。
您正在使用什么版本。它适用于2.6.5版本的新旧风格类别。 – aaronasterling 2010-09-05 00:46:22