The pythonic approach
Alex Martelli
aleaxit at yahoo.com
Wed Sep 15 13:30:30 EDT 2004
Steven Bethard <steven.bethard at gmail.com> wrote:
...
> I actually wrote code not too long ago that did make a call like .get(x, x).
I consider somedict.get(x, x) a _frequent_ Python idiom!
translate_some_tags = dict(
application='literal',
foobar='barfoo',
whatever='somethingelse',
}
def translate_tag(tagName, attributes_dict):
if tagName == 'emphasis' and attributes.get('role')=='bold':
return 'bold'
else:
return translate_some_tags.get(tagName, tagName)
Isn't that a pretty frequent idiom when doing some minor massaging of
XML markup, for example? (Spare me the passionate defenses of XSLT
please... I'm happy to use XSLT when warranted, but a lot of the XML
processing I do, and I _do_ do a lot of that, is in
Python+pulldom...!-).
> back. I didn't have a function for it in my code, but I don't see why a
> function like:
>
> def getstem(self, word):
> return self.stemdict.get(word, word)
>
> would be so unreasonable.
A very good idea! If tomorrow you need to specialcase something, based
e.g. on RE's, you stick those into getstem (just like I do in
translate_tag for the few cases where a tag's translation may depend on
its attributes), and Bob's your uncle. MUCH better than spreading calls
to get(word,word) all over the place!!!
Alex
More information about the Python-list
mailing list