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 ():
.
There are many. About 2 cases.
python
try:
process(dic[key])
except KeyError:
process(None)
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.
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