Changing the repr() of a class?

Bjorn Pettersen BPettersen at NAREX.com
Thu Apr 18 17:07:42 EDT 2002


> From: Lulu of the Lotus-Eaters [mailto:mertz at gnosis.cx] 
> 
> 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'm assuming you mean something like:

>>> class C(object):
...   def __repr__(klass): return 'class %s' % klass.__class__
...   __repr__ = classmethod(__repr__)
...

Unfortunately, that doesn't work, so I'd stick with Alex' way <wink>

-- bjorn





More information about the Python-list mailing list