开发者

How do I access an object's properties by name?

开发者 https://www.devze.com 2022-12-17 07:28 出处:网络
class a(type): def __str__(se开发者_如何学JAVAlf): return \'aaa\' def __new__(cls, name, bases, attrs):
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?

0

精彩评论

暂无评论...
验证码 换一张
取 消