The pythonic approach

Steven Bethard steven.bethard at gmail.com
Wed Sep 15 12:20:40 EDT 2004


Peter Hansen <peter <at> engcorp.com> writes:
> (My gut tells me that if this were something that was
> required in real code, it would actually be using an
> existing dictionary (not one created with a single
> entry just before the function) and the best code
> would be an inline .get() rather than a function call,
> and that x might not even be its own default, but
> I can't imagine what real code this corresponds to
> so that's all I have to say about that. 

I actually wrote code not too long ago that did make a call like .get(x, x).  
I had a dictionary of word stems, e.g. 
{"invention":"invent", "assistance":"assist", ...} read in from a file.  For 
the particular app, if there was no stem for a word, I wanted to get the word 
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.

I don't know why you would have to "pause to realize" that .get(x, x) returns 
x as the default any more than you'd have to "pause to realize" that .get(x, 
y) returns y as the default...

Steve




More information about the Python-list mailing list