Python __getattr__ 行为?在 ECLIPSE/PyDev 控制台中?

发布于 2024-10-13 07:00:54 字数 580 浏览 5 评论 0原文

以下:

class A(object):
    def __getattr__(self, attr):
        try:
            return self.__dict__[attr]
        except KeyError:
            self.__dict__[attr] = 'Attribute set to string'
            print 'Assigned attribute'
            return self.__dict__[attr]

返回:

obj = A()
obj.foo
Assigned attribute
Assigned attribute
Assigned attribute
'Attribute set to string'

魔法发生在哪里?

(我使用的是 2.6.6)

编辑:感谢您的反馈。事实上,这个问题无法从 Python 命令行本身重现。似乎只有在 Eclipse/PyDev 中使用控制台时才会出现这种情况。

The following:

class A(object):
    def __getattr__(self, attr):
        try:
            return self.__dict__[attr]
        except KeyError:
            self.__dict__[attr] = 'Attribute set to string'
            print 'Assigned attribute'
            return self.__dict__[attr]

returns:

obj = A()
obj.foo
Assigned attribute
Assigned attribute
Assigned attribute
'Attribute set to string'

Where is the magic happening?

(I'm on 2.6.6)

Edit: Thanks for your feedback. Indeed, this problem can't be reproduced from the Python command line itself. It seems that it only occurs when using the console in Eclipse/PyDev.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黯然 2024-10-20 07:00:54

我的代码版本略有不同,可能会有所帮助。

class A(object):
    def __getattr__(self, attr):
        try:
            return self.__dict__[attr]
        except KeyError:
            self.__dict__[attr] = 'Attribute set to string'
            print 'Assigned attribute', attr
            return self.__dict__[attr]

>>> o = A()
>>> o.foo
Assigned attribute foo
'Attribute set to string'

我不知道你如何多次看到“分配的属性”。这是 Python 2.6.6 的情况。

值得指出的是,如果调用 __getattr__,则 try 总是会失败。

I have a slightly different version of your code that might help.

class A(object):
    def __getattr__(self, attr):
        try:
            return self.__dict__[attr]
        except KeyError:
            self.__dict__[attr] = 'Attribute set to string'
            print 'Assigned attribute', attr
            return self.__dict__[attr]

>>> o = A()
>>> o.foo
Assigned attribute foo
'Attribute set to string'

I don't know how you see "Assigned attribute" more than once though. This is with Python 2.6.6.

It's worth pointing out that the try always fails if __getattr__ is called.

叹倦 2024-10-20 07:00:54

这不会发生:

class A(object):
    def __getattr__(self, attr):
        try:
            return self.__dict__[attr]
        except KeyError:
            self.__dict__[attr] = 'Attribute set to string'
            print 'Assigned attribute'
            return self.__dict__[attr]

obj = A()
print obj.foo

给出:

Assigned attribute
Attribute set to string

仅当属性不存在时才调用 __getattr__ !所以 try .. except 每次都会进入 except ...

它相当于:

class A(object):
    def __getattr__(self, attr):
        val = 'Attribute set to string'
        setattr(self, attr, val)
        print 'Assigned attribute'
        return val

That doesn't happen:

class A(object):
    def __getattr__(self, attr):
        try:
            return self.__dict__[attr]
        except KeyError:
            self.__dict__[attr] = 'Attribute set to string'
            print 'Assigned attribute'
            return self.__dict__[attr]

obj = A()
print obj.foo

gives:

Assigned attribute
Attribute set to string

__getattr__ is only called when the attribute does not exist! So the try .. except will go into the except every time ...

It's equivalent to:

class A(object):
    def __getattr__(self, attr):
        val = 'Attribute set to string'
        setattr(self, attr, val)
        print 'Assigned attribute'
        return val
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文