[Python-Dev] dict() enhancement idea?
Barry A. Warsaw
barry@python.org
Thu, 21 Nov 2002 11:01:17 -0500
>>>>> "JvR" == Just van Rossum <just@letterror.com> writes:
JvR> But: no, I simply find the {"key": "value"} syntax sometimes
JvR> inappropriate. Consider the following example:
Me too, and I have something similar in Mailman. It's fine that keys
are limited to identifiers (although I did recently have one small
related bug because of this in a mostly unused corner of the code).
| # idiom 2
| name1 = foo()
| name2 = foo()
| x = template % locals()
| # idiom 3 (after my patch, or with a homegrown function)
| x = template % dict(key1=foo(), key2=baz())
JvR> I find #3 more readable than #1. #2 ain't so bad, but I hate
JvR> it that when you're quickly going over the code it looks like
JvR> there are some unused variables.
I go even a step farther than #2 with my i18n idiom, e.g.
name1 = foo()
name2 = foo()
x = _(template)
Where _() does a sys._getframe() and automatically sucks the locals
and globals for interpolation. Works great, but it gives pychecker
fits. :)
I like both the **kws addition as well as the keywords-as-attributes
conveniences.
-Barry