The pythonic approach

Alex Martelli aleaxit at
Wed Sep 15 19:30:30 CEST 2004

Steven Bethard <steven.bethard at> 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(

def translate_tag(tagName, attributes_dict):
    if tagName == 'emphasis' and attributes.get('role')=='bold':
        return 'bold'
        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

> 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!!!


More information about the Python-list mailing list