class a(type):
def __str__(se开发者_如何学JAVAlf):
return 'aaa'
def __new__(cls, name, bases, attrs):
attrs['cool']='cool!!!!'
new_class = super(a,cls).__new__(cls, name, bases, attrs)
#if 'media' not in attrs:
#new_class.media ='media'
return new_class
class b(object):
__metaclass__=a
def __str__(self):
return 'bbb'
print b
print b()['cool']#how can i print 'cool!!!!'
print b().cool
attrs
in your __new__
method becomes the object's dictionary. Properties of Python objects are referenced with the .
syntax.
print "cool!!!"
Or did I miss something?
精彩评论