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?
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:
But it’s not modifiable:
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).
So to boil it down, the default class implementation for classes is
type, so the default attribute looker-upper for class attributes istype.__getattribute__, and you cannot modify or replacetype.__getattribute__directly, but you can replacetypeusing the metaclass mechanism, and, as in the example above, replace it with a subclass oftypethat has the__getattribute__you want.