2010-01-19 7 views
2
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!!!!' 
+5

사람들이 제목을 읽지 않는 경우를 대비하여 나머지 질문을 반영하도록 노력해야합니다. –

+0

인쇄 Foo 또는 Bar (http://en.wikipedia.org/wiki/Foobar)는 더 차갑습니다! – ep3static

답변

5
print b().cool 

attrs 객체의 사전된다. Python 개체의 속성은 . 구문으로 참조됩니다.

1
print "cool!!!" 

또는 뭔가를 놓쳤습니까? 당신의 __new__ 방법

+0

나는 어떻게 그렇게 투표 득점을 사랑합니다 : (1 * +1) + (4 * -1) = +2 –

+0

나는 당신의 대답을 벗어났습니다. – Erik

관련 문제