Why doesn't Python implement an elif statement in a try statement?

So let's do a quick example.

my_list = [ {"name": "toto", "value": 3}, {"name": "foo", "value": 42}, {"name": "bar", "value": 56} ] def foo(name): try: value = next(e["value"] for e in my_list if e["name"] == name) except StopIteration: print "Uuuh not found." else: if value % 2: print "Odd !" else: print "Even !" 

As you can see, the above code works:

 >>> foo("toto") Odd ! >>> foo("foo") Even ! >>> foo("kappa") Uuuh not found. 

I'm just wondering if there is any specific reason why we cannot use the elif statement with the try as follows:

 try: value = next(e["value"] for e in my_list if e["name"] == name) except StopIteration: print "Uuuh not found." elif value % 2: print "Odd !" else: print "Even !" 

Of course, this would not work, because, as defined in the application documentation , elif not defined. But why? Is there a specific reason (e.g. a bound / unbound variable)? Are there any sources about this?

(Side note: elif-statement tag?)

+5
source share
1 answer

If you ask why then this is a question for python developers, the syntax is clearly defined in the docs that you linked to.

Besides what you are trying to do, obviously everything can be done inside try / except. If you use else, then it applies to try in the same way as when using else with a for loop, I think it makes sense to use only else and not allow elif , elif should behave like switch/case in other languages.

From the old thread to gossamer-threads Why is there no "elif" in try / except? :

Because it makes no sense?

With the exception of exceptions for handling exceptions, else for handling cases when everything is working fine.

Mixing the two does not make sense. The except clause is not a test result; in the else section is. Is the elite supposed to fulfill when there are exceptions, or when there are none? If this is simply an extension to the else clause, then I suppose it doesn’t harm anything, but it adds complexity to the language definition. At the moment in my life I tend to agree with Einstein - to make everything as simple as possible, but not easier. Last place (or at least one of many last places) complexity lies in handling exceptions.

You always need at least one thing, if you use elif, this is just an invalid syntax without. The only thing that cannot be determined is value , you have to move the logic inside try / except using if / else:

 try: value = next(e["value"] for e in my_list if e["name"] == name) if value % 2: print("Odd !") else: print("Even !") except StopIteration: print("Uuuh not found.") 

If value = next... errors are met, your print("Uuuh not found.") Will be executed, if not yours, then your if / else will be.

You may have several elifs, but you should start with if:

 if something: ... elif something_else: .... elif ..... 

Another option is to use the default value with next , and then use if/elif/else , if we don’t get a name that matches the next, it will return None , so we will check if value is not None if we print "Uuuh not found." otherwise we get a matching name, so go to elif / else:

 value = next((e["value"] for e in my_list if e["name"] == name), None) if value is None: print("Uuuh not found.") elif value % 2: print("Odd !") else: print("Even !") 
+3
source

All Articles