[Python-ideas] Enhancing vars()

Paul Moore p.f.moore at gmail.com
Tue Dec 13 06:53:44 EST 2016


On 13 December 2016 at 11:28, Nick Coghlan <ncoghlan at gmail.com> wrote:
> Armed with those, the "give me all the attributes from __dir__"
> command would be:
>
>     attrs = dict.from_attrs(obj, dir(obj))

Which of course can already be spelled as

    attrs = { attr: getattr(obj, attr) for attr in dir(obj) }

There's obviously a speed-up from avoiding repeated getattr calls, but
is speed the key here?

The advantage of an "enhanced vars" is more likely to be ease of
discoverability, and I'm not sure dict.fromattrs gives us that
benefit. Also, the dict constructor gives a *copy* of the namespace,
where the proposal was for the proxy returned by vars() to provide
update capability (if I understand the proposal correctly).

Paul


More information about the Python-ideas mailing list