Why use "locals()"

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Sun Sep 13 23:18:35 EDT 2009


On Sun, 13 Sep 2009 20:06:51 -0700, Sean DiZazzo wrote:

> I have never used a call to "locals()" in my code.  Can you show me a
> use case where it is valuable and Pythonic?

grep is your friend:



$ grep "locals()" /usr/lib/python2.5/*.py
/usr/lib/python2.5/decimal.py:        for name, val in locals().items():
/usr/lib/python2.5/doctest.py:        return __import__(module, globals(), locals(), ["*"])
/usr/lib/python2.5/profile.py:        p.runctx('f(m)', globals(), locals())
/usr/lib/python2.5/pydoc.py:            docloc = '<br><a href="%(docloc)s">Module Docs</a>' % locals()
/usr/lib/python2.5/smtpd.py:        mod = __import__(classname[:lastdot], globals(), locals(), [""])




-- 
Steven



More information about the Python-list mailing list