class a(type):
def __str__(self):
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!!!!'
views:
172answers:
2I love how SO counts votes: (1 * +1) + (4 * -1) = +2
2010-01-19 08:24:05
I got a laugh outta your answer
Erik
2010-01-19 09:00:19
+4
A:
print b().cool
attrs in your __new__ method becomes the object's dictionary. Properties of Python objects are referenced with the . syntax.
jleedev
2010-01-19 08:15:25