23.10.19 14:00, Steven D'Aprano пише:
On Wed, Oct 23, 2019 at 01:42:11PM +0300, Serhiy Storchaka wrote:
23.10.19 13:08, Steven D'Aprano пише:
But the advantage of changing the syntax is that it becomes the One Obvious Way, and you know it will be efficient whatever version or implementation of Python you are using.
There is already the One Obvious Way, and you know it will work whatever version or implementation of Python you are using.
Your "One Obvious Way" is not obvious to me. Should I write this:
# This is from actual code I have used. ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty", "twenty-one", "twenty-two", "twenty-three", "twenty-four" "twenty-five", "twenty-six", "twenty-seven", "twenty-eight", "twenty-nine", "thirty"]
"""zero one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen twenty twenty-one twenty-two twenty-three twenty-four twenty-five twenty-six twenty-seven twenty-eight twenty-nine thirty""".split()
I've been told by people that if I use the first style I'm obviously ignorant and don't know Python very well, and by other people that the second one is a hack and that I would fail a code review for using it.
So please do educate me Serhiy, which one is the One Obvious Way that we should all agree is the right thing to do?
If you need a constant number, the most obvious way is to write it as a number literal, not int('123'). If you need a constant string, the most obvious way is to write it as a string literal, not bytes([65, 66]).decode(). If you need a list of constant strings, the most obvious way is to write it as a list display consisting of string literals. It works in all Python versions.
The second way works too in all actual Python versions (starting from 1.6), and nobody will beat you if you use it in your code. It can save you few keystrokes. But it is less obvious and less general.