python - Why does a metaclass not have access to the attributes inhereited from a subclass of a class defined by the metaclass? -
class foo
defined metaclass meta
. metaclass loops on class attributes , prints them screen.
class bar
subclasses foo
. however, metaclass not print inherited attributes bar.
why doesn't metaclass have access foo
's attributes inherited in bar
? not understanding python's metaclass system?
here sample code in 2.7
:
class meta(type): def __init__(cls, name, bases, attrs): print "bases = {}".format(bases) items = {k:v k,v in attrs.iteritems() if not k.startswith('__')} k,v in items.iteritems(): print k, v class foo(object): __metaclass__ = meta hi = 1 # prints: # bases = (<type 'object'>,) # hi 1 class bar(foo): pass # prints: # bases = (<class '__main__.foo'>,) foo.hi #prints 1 bar.hi #prints 1
the attrs
parameter __init__
contains attributes for class, not bases.
a bar
object not have attribute hi
. instead, when ask bar.hi
lookup start @ bar
, find out doesn't have hi
, in base foo
find it.