Will the day come when Python can have an except expression?

Expressions and sentences

Many programming languages, including Python, have a distinction between statements and expressions. The details, as I'll give it to others (because I'm not very familiar with the language), are very rough.

It can be said. If you don't have a desire to write your code short, you can do either, but to write it short, it's better to have as many expressions as possible, pushing as many into one expression. On the other hand, if you make everything an expression, you can easily write complicated and strange code, and I hate it in some languages. Python is a language that dislikes it, for example assignments are statements rather than expressions. So you can't write code like ʻa = (b = 1) + (c = 2) . There is no such expression as ʻa = i ++ . Functions can also be created with lambda expressions, but statements cannot be written inside lambda expressions, and only functions that can be represented by a single expression can be created. If you want to create such a complicated function, the idea is to define it properly with def hogehoge ():.

Why you want an except expression

There are many. About 2 cases.

  1. Dictionary reference

python


try:
    process(dic[key])
except KeyError:
    process(None)
  1. Int conversion of character string

python


try:
    process(int(s))
except ValueError:
    process(0)

The dictionary reference of 1 can be rewritten with if.

if key in dic:
    process(dic[key])
else:
    process(None)

It can also be written as an expression using the ternary operator. process(dic[key] if key in dic else None) If you want to do int conversion of the character string of 2, it can be rewritten with if. process(int(s) if s[0] in '-0123456789' and all(c in '0123456789' for c in s) else 0) But do you do this? I'm feeling. It would be even more annoying if it was float instead of int.

Reaction of PEP 463 and Guido

Therefore, a PEP was proposed in March 2014 to add an except expression. http://legacy.python.org/dev/peps/pep-0463/

lst = [1, 2]
value = (lst[2] except IndexError: "No value")

Yup. This is easy to understand. With this, the int of the character string is also process(int(s) except IndexError: 0) Can be written simply.

However, Python's creator, Guido, said that as of March 2014, the PEP was well-studied and the grammar was well-developed, but the proposal itself would be rejected. https://mail.python.org/pipermail/python-dev/2014-March/133118.html

But the thing I can't get behind are the motivation and rationale. I don't think that e.g. dict.get() would be unnecessary once we have except expressions, and I disagree with the position that EAFP is better than LBYL, or "generally recommended" by Python.

(Miscellaneous translation) But what I disagree with is the motive and rationale. For example, I don't think dict.get was unnecessary if there was an except expression. I also disagree with the idea that EAFP (\ * 1) is better than LBYL (\ * 2) or is "generally recommended" in Python.

(\ * 1) Abbreviation for it's Easier to Ask Forgiveness than Permission (it's easier to ask for permission than to get permission). In other words, like the try-except syntax, if you try and fail, take another method. (\ * 2) Look Before You Leap: The cane that doesn't fall, that is, make sure that if does not fail in advance.

I think it would be convenient to have it. After all, isn't it adopted?

Recommended Posts

Will the day come when Python can have an except expression?
You will be an engineer in 100 days --Day 29 --Python --Basics of the Python language 5
You will be an engineer in 100 days --Day 33 --Python --Basics of the Python language 8
You will be an engineer in 100 days --Day 26 --Python --Basics of the Python language 3
You will be an engineer in 100 days --Day 35 --Python --What you can do with Python
You will be an engineer in 100 days --Day 32 --Python --Basics of the Python language 7
You will be an engineer in 100 days --Day 28 --Python --Basics of the Python language 4
You will be an engineer in 100 days --Day 27 --Python --Python Exercise 1
You will be an engineer in 100 days --Day 34 --Python --Python Exercise 3
You will be an engineer in 100 days --Day 31 --Python --Python Exercise 2
I got an AttributeError when mocking the open method in python
Have python check if the string can be converted / converted to int
Check the behavior when assigning Python
Have python read the command output
Python Note: When you want to know the attributes of an object
You will be an engineer in 100 days ――Day 24 ―― Python ―― Basics of Python language 1
The eval () function that calculates a string as an expression in python
You will be an engineer in 100 days ――Day 30 ―― Python ―― Basics of Python language 6
You will be an engineer in 100 days ――Day 25 ―― Python ―― Basics of Python language 2
[Python3] Code that can be used when you want to change the extension of an image at once