Why ELIF?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sun Oct 11 10:07:27 CEST 2009


On Sat, 10 Oct 2009 23:47:38 -0700, metal wrote:

> I wonder the reason for ELIF. it's not aligned with IF, make code ugly
> IMHO
> 
> OR maybe better?
> 
> if foo == bar:
> 	...
> or foo == baz:
> 	...
> or foo == bra:
> 	...
> else:
> 	...


`or` has another meaning in Python, and many other languages:

flag = len(mystring) > 10 or count < 50

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().



-- 
Steven



More information about the Python-list mailing list