[Python-Dev] dir, part 2 (original) (raw)

Guido van Rossum guido at python.org
Fri Nov 10 20:30:57 CET 2006


On 11/10/06, Fredrik Lundh <fredrik at pythonware.com> wrote:

Guido van Rossum wrote:

> No objection on targetting 2.6 if other developers agree. Seems this > is well under way. good work! given that dir() is used extensively by introspection tools, I'm not sure I'm positive to a dir that overrides the standard dir() behaviour. adding to the default dir() list is okay, re- placing it is a lot more questionable.

I think that ought to go into the guidlines for what's an acceptable dir implementation. We don't try to stop people from overriding add as subtraction either.

(what about vars(), btw?)

Interesting question! Right now vars() and dir() don't seem to use the same set of keys; e.g.:

class C: pass ... c = C() c.foo = 42 vars(c) {'foo': 42} dir(c) ['doc', 'module', 'foo']

It makes some sense for vars(x) to return something like

dict((name, getattr(x, name)) for name in dir(x) if hasattr(x, name))

and for the following equivalence to hold between vars() and dir() without args:

dir() == sorted(vars().keys())

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



More information about the Python-Dev mailing list