In a recent project I try to do something like this (more complex, but the same result):
class MetaA(type):
def __new__(cls, name, args, kwargs):
print kwargs["foo"]
return type.__new__(cls, name, args, kwargs)
class A(object):
__metaclass__ = MetaA
foo = "bar"
class B(A):
pass
I get this:
bar
Traceback (most recent call last):
File "C:\Users\Thorstein\Desktop\test.py", line 10, in <module>
class B(A):
File "C:\Users\Thorstein\Desktop\test.py", line 3, in __new__
print kwargs["foo"]
KeyError: 'foo'
Are class attributes not inherited? If so, is there any workaround possible in a similar framework to the above?
EDIT: Might be easier to see what I mean using an actual (simplified) example from the program..
class GameObjectMeta(type):
def __new__(cls, name, bases, attrs):
attrs["dark_color"] = darken(*attrs["color"])
return type.__new__(cls, name, bases, attrs)
class GameObject(object):
__metaclass__ = GameObjectMeta
color = (255,255,255) # RGB tuple
class Monster(GameObject):
pass
Basically, want to run a function on the base color to make a darker one that's saved in the class (multiple instances of the class will want the same color, but there will be a longer class hierarchy). I hope this'll make more sense..