Changing the repr() of a class?
Emile van Sebille
emile at fenx.com
Thu Apr 18 17:40:12 EDT 2002
"Lulu of the Lotus-Eaters" <mertz at gnosis.cx> wrote in message
news:mailman.1019162916.19664.python-list at python.org...
> Alex Martelli <aleax at aleax.it> wrote previously:
> |class FunkyReprMeta(type):
> | def __repr__(cls):
> | return "My wonderful class %s" % cls.__name__
> |class FunkyReprObject:
> | __metaclass__ = FunkyReprMeta
> |class C(FunkyReprObject):
> | "whatever you want here"
>
> I don't really understand what a metaclass buys you here. I think I
> might be missing something important, since Alex proposes it.
>
> However, it appears that one could simply define the __repr__ method
in
> the regular class FunkyReprObject, ignore metaclasses altogether, and
> wind up with the same results. What is better about Alex' approach?
>
I think you missed the point of OP's question...
>>> class C:
... pass
...
>>> C
<class __main__.C at 0x007A9548>
>>> class FunkyReprMeta(type):
... def __repr__(cls):
... return "My wonderful class %s" % cls.__name__
...
>>> class FunkyReprObject:
... __metaclass__ = FunkyReprMeta
...
>>> class C(FunkyReprObject):
... "whatever you want here"
...
>>>
>>> C
My wonderful class C
--
Emile van Sebille
emile at fenx.com
---------
More information about the Python-list
mailing list