[Python-Dev] __dir__, part 2
Georg Brandl
g.brandl at gmx.net
Sat Nov 11 11:20:41 CET 2006
Guido van Rossum wrote:
>> (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())
+1. This is easy and straightforward to explain, better than
"With a module, class or class instance object as argument (or anything else
that has a __dict__ attribute), returns a dictionary corresponding to the
object's symbol table."
Georg
More information about the Python-Dev
mailing list