Why ELIF?

Esmail ebonak at hotmail.com
Sun Oct 11 17:42:38 CEST 2009


Steven D'Aprano wrote:
> 
> By the way, if you're testing a single name against a series of 
> alternatives, it is often better to look up the value in a dictionary:
> 
> table = {bar: 23, baz: 42, boop: 73, beep: 124}
> value = table[foo]
> 
> instead of:
> 
> if foo == bar:
>     value = 23
> elif foo == baz:
>     value = 42
> elif ...
> 
> You can even provide a default value by using table.get().

cool .. I hadn't seen that. Not working quite at the 'pythonic' level yet
I am not sure I think it's more readable that the if statement. Also, curious
if the dictionary approach is more efficient.

thanks,
Esmail




More information about the Python-list mailing list