default repr?

Oscar Benjamin oscar.benjamin at bristol.ac.uk
Mon Jul 23 09:14:29 EDT 2012


On 23 July 2012 01:24, Steven D'Aprano <steve+comp.lang.python at pearwood.info
> wrote:

> On Mon, 23 Jul 2012 08:54:00 +1000, Chris Angelico wrote:
>
> > On Mon, Jul 23, 2012 at 8:48 AM, Dan Stromberg <drsalists at gmail.com>
> > wrote:
> >> If a class has defined its own __repr__ method, is there a way of
> >> getting the default repr output for that class anyway?
>
> If the class, call it C, is a subclass of some other class (or classes),
> then there is also the repr of the parent. You can get to that by calling
> parent.__repr__(instance), although there are some subtleties.
>
> In Python 2, there are old-style or classic classes that don't inherit
> from anything else. I don't believe there is any way to get the repr of a
> classic class with no __repr__ method *except* from an instance with no
> __repr__ method. So the answer for C below will be No:
>
> # Python 2.x
> class C:
>     def __repr__(self):
>         return "C()"
>

You coudl always implement repr yourself:

def repr_oldstyle(obj):
    mod = obj.__class__.__module__
    cls = obj.__class__.__name__
    mem = '0x' + hex(id(obj))[2:].zfill(8).upper()
    return '<{0}.{1} instance at {2}>'.format(mod, cls, mem)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120723/d92cbfe9/attachment.html>


More information about the Python-list mailing list