[help] Is it true to call obj.__str__() while executing "print obj"?

Calvin Spealman ironfroggy at gmail.com
Wed Nov 29 21:09:28 EST 2006


On 11/29/06, Tommy Zong <tzong at sigma-rt.com> 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: tzong at sigma-rt.com
>  MSN: metalzong at 163.com
> --
> 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/



More information about the Python-list mailing list