Why use "locals()"

Sean DiZazzo half.italian at gmail.com
Mon Sep 14 05:26:06 CEST 2009


On Sep 13, 8:18 pm, Steven D'Aprano
<ste... at REMOVE.THIS.cybersource.com.au> wrote:
> 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

That is not a use case.  I still don't understand!

PS.  I know how to use grep.

~Sean



More information about the Python-list mailing list