On Mon, 21 May 2012 08:37:29 -0400, Roy Smith 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?

Definitely the dictionary lookup.

Rather than convert list CHOICES to a dict every time, you might convert 
it to a dict *once*, then just write:

return CHOICES.get(self.level, "???")

> Assume the list
> of choices is short enough that the cost of building a temporary dict on
> each call is negligible.

Negligible or not, why bother?

Not that it really matters -- if you have a good reason for CHOICES to 
remain a list, still stick with the dict lookup rather than a explicit 
loop.


-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to