Mixin 类来跟踪属性请求 - __attribute__ 递归

发布于 2024-09-12 03:38:29 字数 420 浏览 5 评论 0原文

我正在尝试创建一个必须是其他类的超类的类,跟踪它们的属性请求。我想过使用“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 技术交流群。

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

发布评论

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

评论(2

浅唱ヾ落雨殇 2024-09-19 03:38:29

试试这个:

class Mixin(object):
    def __getattribute__ (self, attr):
        print self, "getting", attr
        return object.__getattribute__(self, attr)

如果您仍然遇到递归问题,这是由您没有向我们展示的代码引起的

>>> class Mixin(object):
...     def __getattribute__ (self, attr):
...         print self, "getting", attr
...         return object.__getattribute__(self, attr)
...
>>> Mixin().__str__
<__main__.Mixin object at 0x00B47870> getting __str__
<method-wrapper '__str__' of Mixin object at 0x00B47870>
>>> Mixin().foobar
<__main__.Mixin object at 0x00B47670> getting foobar
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in __getattribute__
AttributeError: 'Mixin' object has no attribute 'foobar'
>>>

,这是与 Bob 的 Mylist 结合使用的结果

>>> class Mylist(Mixin):
...     def __init__ (self, lista):
...         if not type (lista) == type (""):
...             self.value = lista[:]
...     def __add__ (self,some):
...         return self.value + some
...     def __getitem__ (self,item):
...         return self.value[item]
...     def __getslice__ (self, beg, end):
...         return self.value[beg:end]
...
>>> a=Mylist([1,2])
>>> a.value
<__main__.Mylist object at 0x00B47A90> getting value
[1, 2]

Try this:

class Mixin(object):
    def __getattribute__ (self, attr):
        print self, "getting", attr
        return object.__getattribute__(self, attr)

If you are still getting recursion problems, it is caused by code you haven't shown us

>>> class Mixin(object):
...     def __getattribute__ (self, attr):
...         print self, "getting", attr
...         return object.__getattribute__(self, attr)
...
>>> Mixin().__str__
<__main__.Mixin object at 0x00B47870> getting __str__
<method-wrapper '__str__' of Mixin object at 0x00B47870>
>>> Mixin().foobar
<__main__.Mixin object at 0x00B47670> getting foobar
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in __getattribute__
AttributeError: 'Mixin' object has no attribute 'foobar'
>>>

And here is the result when combined with Bob's Mylist

>>> class Mylist(Mixin):
...     def __init__ (self, lista):
...         if not type (lista) == type (""):
...             self.value = lista[:]
...     def __add__ (self,some):
...         return self.value + some
...     def __getitem__ (self,item):
...         return self.value[item]
...     def __getslice__ (self, beg, end):
...         return self.value[beg:end]
...
>>> a=Mylist([1,2])
>>> a.value
<__main__.Mylist object at 0x00B47A90> getting value
[1, 2]
柠檬 2024-09-19 03:38:29

这是代码:

from Es123 import Mixin
class Mylist(Mixin):
    def __init__ (self, lista):
        if not type (lista) == type (""):
            self.value = lista[:]
    def __add__ (self,some):
        return self.value + some
    def __getitem__ (self,item):
        return self.value[item]
    def __getslice__ (self, beg, end):
        return self.value[beg:end]
a = Mylist ([1,2])
a.value

然后python返回“RuntimeError:超出最大递归深度”

This is the code:

from Es123 import Mixin
class Mylist(Mixin):
    def __init__ (self, lista):
        if not type (lista) == type (""):
            self.value = lista[:]
    def __add__ (self,some):
        return self.value + some
    def __getitem__ (self,item):
        return self.value[item]
    def __getslice__ (self, beg, end):
        return self.value[beg:end]
a = Mylist ([1,2])
a.value

Then python returns "RuntimeError: maximum recursion depth exceeded"

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