On 23 July 2012 01:24, Steven D'Aprano <steve+comp.lang.pyt...@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 <drsali...@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)
-- http://mail.python.org/mailman/listinfo/python-list