S
Sean DiZazzo
I have never used a call to "locals()" in my code. Can you show me a
use case where it is valuable and Pythonic?
~Sean
use case where it is valuable and Pythonic?
~Sean
I have never used a call to "locals()" in my code. Can you show me a
use case where it is valuable and Pythonic?
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(), [""])
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(), [""])
That is not a use case. I still don't understand!
PS. I know how to use grep.
I have never used a call to "locals()" in my code. Can you show me a
use case where it is valuable and Pythonic?
use case where it is valuable and Pythonic?
def print_item(item):
description = textwrap.fill(item.description, 40)
short = item.description.split('\n', 1)[0]
code = str(item.id).zfill(6)
print "%(code)s %(short)s\n%(description)s\n" % locals()
Transferring arguments:
def foo(some, long, list, of, arguments):
additional = 5
return other(**locals())
Defining properties:
class ColourThing(object):
@apply
def rgb():
def fset(self, rgb):
self.r, self.g, self.b = rgb
def fget(self):
return (self.r, self.g, self.b)
return property(**locals())
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(), [""])That is not a use case. I still don't understand!
Look at the source code to find out what they're doing with the
information they extract from locals(), and why.
For instance, profile should be obvious -- debuggers and profilers often
need to see the values of local names.
pydoc is using the fairly common idiom of injecting the values of
variables into a string. Personally, I don't see why it uses this idiom:
docloc = 'something'
docloc = '%(docloc)s' % locals()
instead of this:
docloc = 'something'
docloc = '%s' % docloc
but for more complicated cases, the first idiom is much simpler.
decimal seems to be using locals() to avoid this anti-pattern:
def __init__(self, a, b, c, d, e, f, g, h):
self.a = a
self.b = b
self.c = c
self.d = d
# blah blah blah
self.h = h
and replacing it with:
def __init__(self, a, b, c, d, e, f, g, h):
for name, val in locals().items():
setattr(self, name, val)
del self.self
Another use-case: if you have a tool that documents Python code
automatically, it needs a way to automatically view the values of local
names.
PS. I know how to use grep.
I'm sure you do. But you didn't think of using grep, which is why I made
the suggestion that grepping the standard library is a good tool to use
to search for Pythonic examples of code.
It's not foolproof, e.g. the unittest module is more Java-onic than
Pythonic, but it's a good start.
def print_item(item):
description = textwrap.fill(item.description, 40)
short = item.description.split('\n', 1)[0]
code = str(item.id).zfill(6)
print "%(code)s %(short)s\n%(description)s\n" % locals()
I see the use of that, but according to Zen, "Explicit is better than
implicit."
Transferring arguments:def foo(some, long, list, of, arguments):
additional = 5
return other(**locals())
Why not?:
def foo(**kwargs):
kwargs["additional"] = 5
return other(**kwargs)
Defining properties:class ColourThing(object):
@apply
def rgb():
def fset(self, rgb):
self.r, self.g, self.b = rgb
def fget(self):
return (self.r, self.g, self.b)
return property(**locals())
So really it's just a short hand. But it's against the Zen! Explicit
not Implicit! I'm not convinced....then again, I didn't look at the
source code of the standard libraries.
Gabrielle's
Pythonic things don't adhere to every Zen, then I would suggest that
Gabrielle's examples are perfectly Pythonic shortcuts. But if you
don't want to use them, you don't have to, nobody is forcing you.
Sean DiZazzo said:def print_item(item):
description = textwrap.fill(item.description, 40)
short = item.description.split('\n', 1)[0]
code = str(item.id).zfill(6)
print "%(code)s %(short)s\n%(description)s\n" % locals()
I see the use of that, but according to Zen, "Explicit is better than
implicit."
Sean DiZazzo <[email protected]> wrote:
What I'm not clear about is under what circumstances locals() does
not produce the same result as vars() .
En Tue, 15 Sep 2009 11:18:35 -0300, Sion Arrowsmith
What I'm not clear about is under what circumstances locals() does
not produce the same result as vars() .
py> help(vars)
Help on built-in function vars in module __builtin__:
vars(...)
vars([object]) -> dictionary
Without arguments, equivalent to locals().
With an argument, equivalent to object.__dict__.
Gabriel Genellina said:What I'm not clear about is under what circumstances locals() does
not produce the same result as vars() .
py> help(vars)
Help on built-in function vars in module __builtin__:
vars(...)
vars([object]) -> dictionary
Without arguments, equivalent to locals().
With an argument, equivalent to object.__dict__.
You've received other answers, but just purely from the 'zen' perspective, thereI have never used a call to "locals()" in my code. Can you show me a
use case where it is valuable and Pythonic?
Sion said:Gabriel Genellina said:py> help(vars)What I'm not clear about is under what circumstances locals() does
not produce the same result as vars() .
Help on built-in function vars in module __builtin__:
vars(...)
vars([object]) -> dictionary
Without arguments, equivalent to locals().
With an argument, equivalent to object.__dict__.
Meh, that's what I get for trying to make sense of
http://docs.python.org/library/functions.html#vars
-- which implies the above, but isn't explicit about
it -- rather than using help. In particular, the docs
for locals talk about the inclusion of free variables
in functions, which is absent for vars, which might
imply that there is a difference.
Thanks for your explanation Steven. I see how it can be valuable, but
it seems to always break the second rule of Zen. I don't really want
to get into the code of debuggers, but I guess I can see how they
might have no other way to know what local variables have been set.
~Sean
The zeroth "rule" of zen is "don't take the rules so seriously".
"They're really more like guidelines anyway."
Regards,
~simon
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.