script question

Peter Otten __peter__ at
Sat Apr 18 09:52:26 CEST 2009

python at wrote:

> Peter,
>> Another eval-free variant:
>> [x() for x in vars().values() if hasattr(x, "_included")]
>> If you use getattr(x, "_included", False) instead of hasattr()
>> you can "un-include" functions with ...
> YES! That's what I was struggling to do with my in-elegant use of
> eval(), eg. replacing eval() to expand dir() strings with a dictionary
> of local objects.
> Question: why vars() vs. globals()? My tests in IDLE (Python 2.6) show
> vars() and globals() returning the same items.

Indeed, it doesn't matter here.

I chose vars() because it uses the same logic as dir(), i. e. vars().keys()
should give you the same names as dir(). Note that both vars() and dir()
give you the same namespace as locals() which just happens to be the same
as globals() on the module level. Inside a function you have to use
globals() or vars(module) to access the module's global namespace.


More information about the Python-list mailing list