FEEDBACK WANTED: Type/class unification

Guido van Rossum guido at zope.com
Sun Jul 29 18:53:31 EDT 2001


> > Yes, it's a lot less useful than you seem to think.  If you are
> > interested in documentation, it's better to use something like pydoc
> > which gives much more details.
> 
> Major: Pydoc seems only useful for a small subset of types and classes. 

I was going to fix that.  It currently seems to believe only classic
classes and instances can be introspected using __class__ etc.

> Major: I use dir() in object browsers and other graphical constructs. I
> can't use pydoc for that. I don't see an obvious dir-like function in
> inspect.py.

An object browser is a big app.  Surely it can implement the simple
rules for finding all the attributes.  As a potential user of a GUI
object browser, I would like it to tell me not just which attributes
the class defines but also which ones are inherited and from where;
and information about which ones overload which other ones, etc.

> Minor: It returns so much prose information that I might prefer to use
> dir() to just get the spelling or name of a function.

That's at the >>> prompt right?

> Consider this traceback and you'll see why I still depend on dir()
> 
> >>> a=urllib.urlopen("file:///c|/autoexec.bat")
> >>> type(a)
> <type 'instance'>
> >>> dir(a)
> ['fileno', 'fp', 'headers', 'read', 'readline', 'readlines', 'url']

You're very lucky that this gives the information you want.  These all
happen to be instance variables.

> >>> help(a)
> Help on instance of addinfourl:
> 
> <addinfourl at 16411468 whose fp = <open file 'C:\autoexec.bat', mode
> 'rb' at 00
> F7EB28>>

I think help() should be improved here.

> >>> m = re.match("abc","abcdef")
> >>> type(m)
> <type 'SRE_Match'>
> >>> dir(m)
> ['end', 'expand', 'group', 'groupdict', 'groups', 'span', 'start']
> >>> help(m)
> Help on SRE_Match:
> 
> <SRE_Match object>

Clearly help() can stand improvement.

> > Right now, pydoc doesn't understand the new types yet, but eventually,
> > before 2.2 final, pydoc (or more likely inspect.py) will be able to
> > introspect according to PEP 252.
> 
> Maybe that will be enough to help me for my object-browsing
> applications. And perhaps pydoc will be reliable enough for me to give
> up on dir(x).

I repeat: your object-browsing apps should implement the PEP 252
protocol.

> > ....
> > Hm, I'd like you to tell me more about how you use dir().  I find
> > getting the entire list of attributes overwhelming.
> 
> I use dir(x) as a compressed and somewhat more reliable pydoc that works
> great for modules and built-in types and works poorly for instances.

At the >>> prompt, right?  I don't believe in adding built-ins to make
this mode truly self-sufficient.  That ought to be a function of the
IDE.  (In that sense help() is already against my philosophy -- I
don't find it very useful at the >>> prompt.  I much prefer pydoc's
server / webbrowser mode.)

> What do *you* use dir(x) for? When do you want to introspect an instance
> and you care about instance data but not class data and functions that
> are instance attributes but not true methods.

I like to know which attrs are ivars and which ones are methods, so I
prefer to do dir() on the instance to see what ivars it has, and dir()
on the class to see methods.  If dir() gave me the entire list of
ivars and methods it would be overwhelming and less useful.

--Guido van Rossum (home page: http://www.python.org/~guido/)




More information about the Python-list mailing list