Mixin 类来跟踪属性请求 - __attribute__ 递归
我正在尝试创建一个必须是其他类的超类的类,跟踪它们的属性请求。我想过使用“getattribute”来获取所有属性请求,但它会生成递归:
class Mixin(object):
def __getattribute__ (self, attr):
print self, "getting", attr
return self.__dict__[attr]
我知道为什么我会得到递归:它是为了 self.dict 调用,它让人想起 > 递归获取属性。我尝试按照其他帖子中的建议更改 "return object.__getattribute__(self,attr)"
中的最后一行,但会调用递归。
I'm trying to create a class which must be superclass of others, tracing their attribute requests. I thought of using "getattribute" which gets all attribute requests, but it generates recursion:
class Mixin(object):
def __getattribute__ (self, attr):
print self, "getting", attr
return self.__dict__[attr]
I know why I get recursion: it's for the self.dict call which recalls getattribute recursively. I've tryied to change last line in "return object.__getattribute__(self,attr)"
like suggested in other posts but recursion is recalled.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
如果您仍然遇到递归问题,这是由您没有向我们展示的代码引起的
,这是与 Bob 的
Mylist
结合使用的结果Try this:
If you are still getting recursion problems, it is caused by code you haven't shown us
And here is the result when combined with Bob's
Mylist
这是代码:
然后python返回“RuntimeError:超出最大递归深度”
This is the code:
Then python returns "RuntimeError: maximum recursion depth exceeded"