Martin> SyntaxError: invalid syntax
Martin> Hence the work-around:
>>> make_dict(foo=1, bar=2, class_=3)
{'class_': 3, 'foo': 1, 'bar': 2}
Martin> All of which looks like a stupidly clever hack to me, and the
Martin> need to decorate the names with underscores to sidestep keywords
Martin> highlights the marginality of the gain. This is not the one
Martin> [obvious] way to write a dictionary literal!
So handle that in the attrs function:
def attrs(**kwargs):
d = {}
for k in kwargs:
d[k.rstrip('_')] = kwargs[k]
return d
You can probably think of a suitable workaround/hack/"oh jeez, protect me
from that" to allow 'xml:lang' as well:
def attrs(**kwargs):
d = {}
for k in kwargs:
newk = k.rstrip('_').replace('__', ':')
d[newk] = kwargs[k]
return d
Does the expected thing:
>>> attrs(class_='foo', xml__lang='english')
{'xml:lang': 'english', 'class': 'foo'}
It's a bit ugly, but is it less ugly than the alternatives? What are the
alternatives?
Skip