Making a list-inhereted object return standard repr?

Jason Orendorff jason at jorendorff.com
Sun Feb 3 23:22:10 EST 2002


Timothy Delaney wrote:
> This brings up the interesting question of whether there should be a an
> object method which returns the "canonical repr" of an object i.e. the
> <module.Class object at address> or possibly even a builtin method (so it
> would work with classic classes too).

object.__repr__() works for everything.

If you are working with classic classes, you might prefer this:

def basic_repr(x):
    t = type(x)
    try:
        cls = x.__class__
    except AttributeError:
        pass
    else:
        if t is not cls:
            return t.__repr__(x)  # classic instance
    return object.__repr__(x)    # anything else

This picks up the name of the class for classic instances;
object.__repr__() in that case would return something like
'<instance object at 0x00903888>' which isn't always helpful.

## Jason Orendorff    http://www.jorendorff.com/




More information about the Python-list mailing list