Thanks. Please refer to following snipt: 1 >>> class Printable(type): 2 ... def __str__(cls): 3 ... return "This is class %s" % cls.__name__ 4 ... 5 >>> class C(object): 6 ... __metaclass__ = Printable 7 ... 8 >>> 9 >>> C.__str__ 10 <slot wrapper '__str__' of 'object' objects> 11 >>> 12 >>> print C 13 This is class C
I see, name resolving will follow instance, bases (determined by meta-method mro() ), the last is it's metaclasse. Hence, line 10 shows the " slot wrapper '__str__' of 'object' objects" by I was surprised when I saw line 13 - "print" statement calls str() and the latter one calls obj.__str__() but it seems "obj.__str__" is different from "obj.__str__()" and I was confused by this difference. - Tommy -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Calvin Spealman Sent: Thursday, November 30, 2006 10:09 AM To: [EMAIL PROTECTED]; python-list@python.org Subject: Re: [help] Is it true to call obj.__str__() while executing "printobj"? On 11/29/06, Tommy Zong <[EMAIL PROTECTED]> wrote: > > > > > > > Hi, > > > > I am learning metaclass by reading "Metaclass programming in Python, Part > 2". I have a question as following and had tried to search from internet and > also have read the article "Unifying types and classes in Python 2.2" but > failed to get satisfied answer. Could you please give me any comments to > enlighten me? Thank you very much. > > > > {{{ > > # > > # inheritance inheritance > > # object -------------> type -------------> Printable(Printable.__str__) > > # | . > > # | . > instantiation > > # | . > > # | v > > # ----------------------------------------> C(?) > > # . > > # . > instantiation > > # . > > # v > > # c(?) > > # > > >>> class Printable(type): > > ... def __str__(cls): > > ... return "This is class %s" % cls.__name__ > > ... > > >>> class C(object): > > ... __metaclass__ = Printable > > ... > > >>> print C # > equivalent to print Printable.__str__(C) > > This is class C > > >>> c = C() > > >>> print c # > equivalent to print C.__str__(c) > > <__main__.C object at 0x1870dacc> > > > > >>> C.__str__ > > <slot wrapper '__str__' of 'object' objects> > > >>> print C > > This is class C > > }}} > > > > The question is why Printable.__str__ is invoked while executing "print C" > but "C.__str__" shows it is resolved as "objct.__str__"? > > > > Wish I can get reply from you. Really thanks. > > > > Best Regards, > > > Tommy Zong > Chengdu Jiehua Technologies Co, Ltd. > Tel: 86-28-85148500-654 > Mail: [EMAIL PROTECTED] > MSN: [EMAIL PROTECTED] > -- > http://mail.python.org/mailman/listinfo/python-list > > You are defining a metaclass, a class, and an instance of this class. You are defining a method in the metaclass. When the name is looked up on any object, if the object has no attribute of that name (no __str__ in this case) the search moves to its type, and from there to each type in the MRO (method resolution order). When you follow through the lookup rules, from object to type to supertypes, you see that none of the metaclasses for any of the classes in your object's MRO comes into play. Those are types of the types. They come into play when you lookup an attribute on the types themselves only, not on the instances of those types. That is why you see the bahavior you do. Of course, it wouldnt make sense for this method to be used with the instances, because they aren't classes, so the message would be incorrect. -- Read my blog! I depend on your acceptance of my opinion! I am interesting! http://ironfroggy-code.blogspot.com/ -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list