加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python的getattr被调用两次?

发布时间:2020-12-20 12:17:04 所属栏目:Python 来源:网络整理
导读:我使用这个简单的例子来理解 Python的getattr函数: In [25]: class Foo: ....: def __getattr__(self,name): ....: print name ....: ....: In [26]: f = Foo()In [27]: f.barbarbar 为什么酒吧打印两次?使用Python 2.6.5. 解决方法 我认为这是由于IPython
我使用这个简单的例子来理解 Python的getattr函数:

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

In [26]: f = Foo()

In [27]: f.bar
bar
bar

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

解决方法

我认为这是由于IPython.

要“修复”它,你必须禁用自动调用:%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.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读