【问题标题】:Override -Class Attribute- getter覆盖 -Class Attribute- getter
【发布时间】:2012-02-28 15:22:37
【问题描述】:

我正在像这样定义一个 Debug 类:

_debug = False

class Debug:
    DrawOutlines = True
    InvinciblePlayer = True

我想重写Debug 类,这样如果_debug 为False,任何Debug 的类属性(存在)都将为False。我应该重写什么__function__ 以更改访问类属性的方式?

编辑:

我知道简单地覆盖 __getattribute__ 不适用于类属性:

>>> _debug = False
False
>>> class Debug:
...     DrawOutlines = True
...
...     def __getattribute__(self, name):
...         return _debug and object.__getattribute__(self, name)
...
>>> Debug.DrawOutlines
True
>>> Debug.cake
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'Debug' has no attribute 'cake'

这是我需要元类的情况吗?

【问题讨论】:

  • 如所写,您的 __getattribute__ 方法不会被调用,因为 Debug 不是从 object 继承的,即它是一个旧式类,__getattribute__ 仅适用于新式类。但是,即使修复了这个问题,您的真正问题仍然存在:将调用该方法来查找 Debug 实例的属性,但不会调用 Debug 类。
  • @metamat Python 3.x 类自动继承自 object。

标签: python python-3.x


【解决方案1】:

是的,您需要一个元类,因为如果您在 Debug 类中定义 __getattribute__(请注意,它必须是一个新样式的类),Python 将调用它来针对 Debug 实例进行属性查找,但不会针对属性针对 Debug 本身的查找。

这是有道理的,因为 Debug.__getattribute__ 被定义为对 Debug 实例进行操作,而不是对 Debug 类进行操作。 (你可以想象定义一个类方法__getattribute__,但我找不到任何证据表明 Python 有任何机制可以调用这样的东西。)

我在这里想到的第一件事是添加另一个__getattribute__,Python会在其中查找Debug类属性,即在Debug类是实例的类中:Debug.__class__.__getattribute__。

这确实存在并且可以按您的预期工作:

>>> Debug.__class__.__getattribute__(Debug, 'Draw')
True
>>> Debug.__class__.__getattribute__(Debug, 'Bogus')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in __getattribute__
AttributeError: 'DebugType' object has no attribute 'Bogus'

但它不可修改:

>>> Debug.__class__.__getattribute__ = Debug.__getattribute__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't set attributes of built-in/extension type 'type'

这似乎只是 Python 实现的现实;虽然存在这个概念,但您不能修改内置类型的属性,因此这种方法不起作用。

但是,元类可以解决问题。您可能已经知道如何做到这一点,但对于其他读者,我将举一个例子(this answer 中还有另一个例子,我的回答欠了一些债)。

_debug = True
>>> class DebugType(type):
...     def __getattribute__(self, name):
...             print 'attr lookup for %s' % str(name)
...             return _debug and object.__getattribute__(self, name)
... 
>>> class Debug(object):
...     Draw = True
...     __metaclass__ = DebugType
... 
>>> _debug
False
>>> Debug.Draw
attr lookup for Draw
False
>>> _debug = True
>>> Debug.Draw
attr lookup for Draw
True

所以归结为,类的默认类实现是type,所以类属性的默认属性looker-upper是type.__getattribute__,不能直接修改或替换type.__getattribute__,但是可以替换type 使用元类机制,并如上例所示,将其替换为具有所需 __getattribute__ 的 type 的子类。

【讨论】:

    【解决方案2】:

    您覆盖 __getattribute__ 以拦截 所有 属性访问,或覆盖 __getattr__ 以仅对不存在的属性调用:

    _debug = True
    class A(object):
        def __getattribute__(self, attr):
           if _debug:
               print attr
           return  object.__getattribute__(self, attr)
    

    【讨论】:

    • 这并没有解决它作为类属性的问题,因为它只适用于实例。这里需要元类吗?
    • 是的,如果你写了一个元类,并且写了元类的__getattr__方法,它就可以用于类属性。但是,它可能不适用于特殊的 dunder 方法,例如 __add__ 和 __eq__,因为 Python 的机器会覆盖对这些特殊属性的访问以提高效率
    • 我刚刚使用了一个对象并使用了一个类的单个实例,就像这样。但是,我最初的问题是覆盖类属性,而不是对象属性。如果您编辑此答案以指定我可以使用单个对象(我的示例没有 self.* 属性),或者提供了元类示例,我可以“接受”它。
    猜你喜欢
    • 2015-07-30
    • 1970-01-01
    • 2015-07-10
    • 1970-01-01
    • 2015-05-11
    • 2011-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多