[JPython] dir and java classes

David Ascher da at ski.org
Tue Oct 26 17:28:47 EDT 1999


>>>>> "BAW" == Barry A Warsaw <bwarsaw at cnri.reston.va.us> writes:

BAW> Okay, so what can you do to get the information you want?
BAW> Well, it isn't really that difficult to write your own dir()
BAW> function that, given an instance traversed into the instance's
BAW> class to find method and attribute names.  For completeness you
BAW> probably want to cruise the entire class hierarchy (but maybe
BAW> not).

BAW> So the issue is, why doesn't dir() do this by default?  

I think that it's a very real question.  The purpose of dir() is fairly
ill-defined.  It makes sense for modules and builtin objects in CPython,
but it's quite inconsistent as soon as classes and instances are brought
in.  It's currenty quite hard to explain to novices, mostly because

 1) they need it the most, especially when learning about the language
    in interactive mode, and

 2) understanding what dir() does currently requires a fairly deep
    understanding of the various kinds of objects, certainly deeper than
    should be required (IMHO).

If it were up to me, dir() would return all the things that I can do a
getattr with, with the exclusion of all the names which start with an
underscore.  That's a rule which I can explain in two minutes and
leverages on the other distinction between 'first class' and 'not first
class' attributes in Python (see 'from ... import *', pseudo-private
variables, reserved method names, etc.).

Making a new version of dir() for my own purposes does not work when I'm
teaching "Python", not "David's Python".

--david







More information about the Python-list mailing list