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:
>>> 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With