__setattr__ 在这段 python 代码中做了什么?

发布于 2024-10-22 22:00:56 字数 625 浏览 4 评论 0原文

这是我的代码:

class fun:

    def __getattr__(self,key):
        return self[key]

    def __setattr__(self,key,value):
        self[key] = value+1
a = fun()
a['x']=1
print a['x']

错误是:

AttributeError: fun instance has no attribute '__getitem__'

当我将其更改为:

class fun:

    def __getattr__(self,key):
        return self.key

    def __setattr__(self,key,value):
        self.key = value+1
a = fun()
a.x=1
print a.x

错误是:

RuntimeError: maximum recursion depth exceeded

我能做什么,我想要 2

this is my code:

class fun:

    def __getattr__(self,key):
        return self[key]

    def __setattr__(self,key,value):
        self[key] = value+1
a = fun()
a['x']=1
print a['x']

and the error is :

AttributeError: fun instance has no attribute '__getitem__'

when i change it to :

class fun:

    def __getattr__(self,key):
        return self.key

    def __setattr__(self,key,value):
        self.key = value+1
a = fun()
a.x=1
print a.x

the error is :

RuntimeError: maximum recursion depth exceeded

what can I do, I want get 2

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

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

发布评论

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

评论(3

难如初 2024-10-29 22:00:57

问题是 self.key = ... 调用 __setattr__ ,因此您最终会陷入无限递归。要使用__setattr__,您必须以其他方式访问对象的字段。常见的解决方案有两种:

def __setattr__(self,key,value):
    # Access the object's fields through the special __dict__ field
    self.__dict__[key] = value+1

# or...

def __init__(self):
    # Assign a dict field to access fields set via __[gs]etattr__
    self.attrs = {}

def __setattr__(self,key,value):
    self.attrs[key] = value+1

The problem is that self.key = ... invokes __setattr__, so you end up in an infinite recursion. To use __setattr__, you have to access the object's field some other way. There are two common solutions:

def __setattr__(self,key,value):
    # Access the object's fields through the special __dict__ field
    self.__dict__[key] = value+1

# or...

def __init__(self):
    # Assign a dict field to access fields set via __[gs]etattr__
    self.attrs = {}

def __setattr__(self,key,value):
    self.attrs[key] = value+1
暖树树初阳… 2024-10-29 22:00:57

这是一个错字。

您想要实现特殊方法 __setattr__,而不是没有特殊含义的 __serattr__

It's a typo.

You want to implement the special method __setattr__, and not __serattr__ which has no special meaning.

我的黑色迷你裙 2024-10-29 22:00:57

首先,该方法称为 __setattr__()。这是尝试属性分配的时候。例如,当您执行以下操作时:

self[key] = value+1

...使您的特定调用(无限)递归!

更好的方法是从 object 派生类,即所谓的 新样式类 并调用基类:

class fun(object):

    def __setattr__(self,key,value):
        super(fun, self).__setattr__(key, value + 1)

a = fun()
a.x=1
print a.x

我删除了您的 __getattr__() 实现,因为它没有任何任何价值。

Firstly, the method is called __setattr__(). It is when an attribute assignment is attempted. Such as when you do:

self[key] = value+1

...making your particular call (infinitely) recursive!

A better way to do this would be to derive your class from object, a so-called new-style class and call the base class:

class fun(object):

    def __setattr__(self,key,value):
        super(fun, self).__setattr__(key, value + 1)

a = fun()
a.x=1
print a.x

I removed your __getattr__() implementation, since it did nothing of any value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文