Why use "locals()"
Sion Arrowsmith
sion at viridian.paintbox
Tue Sep 15 10:18:35 EDT 2009
Sean DiZazzo <half.italian at gmail.com> wrote:
>> 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."
I'm having trouble imaginging what a "more explicit" version would be.
I use this idiom a lot for code generation, finding it much more
readable than:
"%s %s\n%s\n" % (code, short, description)
or even:
code + ' ' + short + '\n' + description + '\n'
What I'm not clear about is under what circumstances locals() does
not produce the same result as vars() .
--
\S
under construction
More information about the Python-list
mailing list