A question of style (finding item in list of tuples)

Chris Angelico rosuav at gmail.com
Mon May 21 09:04:03 EDT 2012


On Mon, May 21, 2012 at 10:37 PM, Roy Smith <roy at panix.com> wrote:
> The above code works, but it occurs to me that I could use the much
> shorter:
>
>    def experience_text(self):
>        return dict(CHOICES).get("self.level", "???")
>
> So, the question is, purely as a matter of readability, which would you
> find easier to understand when reading some new code?  Assume the list
> of choices is short enough that the cost of building a temporary dict on
> each call is negligible.  I'm just after style and readability here.

Is there a reason not to keep the dictionary around? That would surely
be the most logical thing to do. (With, of course, no quotes around
self.level.)

But out of your two options, I'd go with the loop. It's clear what
you're doing and doesn't fiddle around with multiple translations.

Chris Angelico



More information about the Python-list mailing list