[Python-ideas] Should we improve `dir`?

Nick Coghlan ncoghlan at gmail.com
Sun Sep 15 03:35:37 CEST 2013

On 15 Sep 2013 09:13, "Terry Reedy" <tjreedy at udel.edu> wrote:
> On 9/14/2013 4:54 PM, Raymond Hettinger wrote:
>> On Sep 14, 2013, at 12:15 PM, David Halter
>> <davidhalter88 at gmail.com
>> <mailto:davidhalter88 at gmail.com>> wrote:
>>> After discussions, I realized that I should probably bring this up in
>>> python-ideas, I think the current implementation can be very confusing
>>> for people trying to introspect classes with `dir`, which is IMHO its
>>> typical use case.
>> The current behavior of dir() is a bit irritating when I am teaching how
>> Python works.
>> That said, the irritation is minor and easily overcome.
>> I would not want to change the behavior and risk breaking
>> existing introspection code (that code is tends to be more
>> fragile and implementation than most other code).
> This was the basis for rejecting http://bugs.python.org/issue19002
> ``dir`` function does not work correctly with classes.
> The proposal obviously broke pydoc and inspect modules.
>> In other words, I just don't think it is worth changing something
>> that has been in-place for a very long long time.  The minor
>> benefit doesn't want the downsides that goes with API churn.
> The other point is that people *usually* call dir(cls) in order to find
the methods they can call in instances of cls:

Right, this is one of the behavioural differences between classes and
instances, and, as far as I am aware, it isn't an accident. Not only that,
but (as Raymond pointed out) even if it was originally an accident it's too
late to change it now.

If introspection tools want to show all the operations available *on the
class*, then they need to include "dir(type(cls))" as well. So there may be
a legitimate feature request for a new section in the pydoc output showing
"class only" methods and attributes.


> --
> Terry Jan Reedy
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130915/27e9f78c/attachment.html>

More information about the Python-ideas mailing list