Making a list-inhereted object return standard repr?

Delaney, Timothy tdelaney at avaya.com
Mon Feb 4 17:36:24 EST 2002


> From: Steve Holden [mailto:sholden at holdenweb.com]
> 
> "Delaney, Timothy" <tdelaney at avaya.com> 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).
> >
> > I know I've wanted to use this in classic classes many 
> times for debugging
> > purposes - I want to be easily able to distinguish 
> instances from each
> > other. Each time I end up having to code it myself.
> >
> Although, of course, even this would not necessarily be 
> unambigous if you
> had code that produced classes from factory functions, as you 
> would then
> have to identify the class by address rather than by name (each class
> returned by the factory having either the same name or no name at all,
> depending on your point of view).

I presume you mean factory functions that have an embedded class definition
...

def factory():

    class Klass:

In that case, you are of course correct. I didn't think of this as it's not
a construct I use very often - in Python I tend to keep my classes at the
module scope.

However, the current __repr__ for such classes fails to distinguish between
instances of different classes produced in this manner as well. I think this
is a case where it is impossible for the interpreter to give you any more
information without reading your mind, and it is therefore incumbent on
either the programmer giving more hints to the interpreter (e.g. writing
their own __repr__, possibly based on the default __repr__) or for Guido to
integrate the mind-reading module into the core library. And I'm afraid this
would be backwards incompatible, as programs started to do what we wanted
them to do, rather than what we told them to do.

Tim Delaney




More information about the Python-list mailing list