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!!!!'
2
  • 5
    Just in case people don't read past the title, you should try to make it reflect the rest of your question. Commented Jan 19, 2010 at 8:15
  • Printing Foo or Bar(en.wikipedia.org/wiki/Foobar) is cooler! Commented Jan 19, 2010 at 9:18

2 Answers 2

5
print b().cool

attrs in your __new__ method becomes the object's dictionary. Properties of Python objects are referenced with the . syntax.

Sign up to request clarification or add additional context in comments.

Comments

1
print "cool!!!"

Or did I miss something?

1 Comment

I got a laugh outta your answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.