Why use "locals()"
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Mon Sep 14 00:24:04 EDT 2009
En Mon, 14 Sep 2009 00:06:51 -0300, Sean DiZazzo <half.italian at gmail.com>
escribió:
> I have never used a call to "locals()" in my code. Can you show me a
> 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())
(example taken from this recent thread
http://groups.google.com/group/comp.lang.python/browse_thread/thread/449eb9b835a472e7/
see also this recipe http://code.activestate.com/recipes/576742/ )
--
Gabriel Genellina
More information about the Python-list
mailing list