4

I'm defining a Debug class like so:

_debug = False

class Debug:
    DrawOutlines = True
    InvinciblePlayer = True

I want to override the Debug class so that if _debug is False, any class attribute of Debug (that exists) would be False. What __function__ do I override in order to change how class attributes are accessed?

Edit:

I know that simply overriding __getattribute__ will not work for class attributes:

>>> _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'

Would this be a case where I would need a metaclass?

Casey Kuball
  • 7,717
  • 5
  • 38
  • 70
  • 1
    As written, your `__getattribute__` method won't be called, because Debug does not inherit from `object`, i.e. it's an old-style class, and `__getattribute__` is only for new-style classes. However, even with that fixed, your real question remains: the method will be called for attribute lookups on instances of Debug, but not for the class Debug. – metamatt Oct 26 '12 at 22:20
  • @metamatt Python 3.x classes automatically inherit from `object`. – Casey Kuball Oct 27 '12 at 18:13

2 Answers2

11

Yes, you need a metaclass, because if you define __getattribute__ in class Debug (which, note, must be a new-style class), Python will invoke that for attribute lookups against instances of Debug, but not for attribute lookups against Debug itself.

That makes sense, because Debug.__getattribute__ is defined to operate on instances of Debug, not the class Debug. (You could imagine defining a classmethod __getattribute__, but I can't find any evidence that Python has any machinery that would invoke such a thing.)

The first thing I thought of here is to add another __getattribute__ where Python would look for it for Debug class attribute lookups, namely, in the class of which the Debug class is an instance: Debug.__class__.__getattribute__.

This does exist and works as you would expect:

>>> 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'

But it's not modifiable:

>>> 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'

This seems to be just a fact of life with the Python implementation; while the concept exists, you're not allowed to modify attributes of builtin types so this approach won't work.

However, metaclasses to the rescue. You probably already know how to do this, but for other readers I'll give an example (and there's another example in this answer, to which my answer owes some debt).

_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

So to boil it down, the default class implementation for classes is type, so the default attribute looker-upper for class attributes is type.__getattribute__, and you cannot modify or replace type.__getattribute__ directly, but you can replace type using the metaclass mechanism, and, as in the example above, replace it with a subclass of type that has the __getattribute__ you want.

Community
  • 1
  • 1
metamatt
  • 13,809
  • 7
  • 46
  • 56
6

You override __getattribute__ to intercept all attribute access, or __getattr__ to get called only for attributes that do not exist:

_debug = True
class A(object):
    def __getattribute__(self, attr):
       if _debug:
           print attr
       return  object.__getattribute__(self, attr)
jsbueno
  • 99,910
  • 10
  • 151
  • 209
  • 1
    This doesn't address the issue of it being a class attribute, as it only works on instances. Would a Metaclass be required here? – Casey Kuball Feb 29 '12 at 20:45
  • 1
    Yes, if you write a metaclass, and writes the metaclass` `__getattr__` method, it would work for class attributes. It may not work for special dunder methods, though, like `__add__` and `__eq__` as Python's machinnery overrides acess to these special attributes in order to be more efficient – jsbueno Mar 01 '12 at 20:33
  • I just ended up using an object and using a single instance of a class just like this. However, my original question was for overriding class attributes, not object attributes. If you edited this answer to either specify that I could use a single object (my example doesn't have `self.*` properties), or provided a meta-class example, I could 'accept' it. – Casey Kuball Mar 02 '12 at 18:59