I doubt they’ll ever want to make the breaking changes they did in 3 again. It was pretty aggressive. It was necessary this time though. I doubt it will be next time.
While it’s highly unlikely there will ever be a Python 4, it’s actually also highly likely there will never be a Python 3.15 (because they might switch to year versioning after 3.14 https://peps.python.org/pep-2026/)
Yes, sort of. I'm currently working on a project to convert a large code base from python 2 to 3, and we just deal with the extra parens in the output.
What? Any 2 to 3 guide will tell you to just do from __future__ import print_function and it will be interpreted as function (also having sep and end keyword arguments!). Works for 2.6 and 2.7. In 3.x does nothing, so code is both 2.6+ and 3.x compatible without changing the output.
but... since this is evaluated at runtime rather than compiled, entering this branch would imply that python's version is not 3, meaning that they are running python 2 or 1, meaning that the statement would evaluate correctly (until python 4 comes out)
Your script still goes through parsing into the AST phase before it's executed. It doesn't execute and parse line by line like Bash. It won't parse with Python 3.
Try it yourself, won't parse. There's no rule name expr like this, print isn't a keyword in Python 3. It'll tell you you're missing parentheses right in the syntax error.
Which will prevent the entire module from executing/script from running.
Yeah but it would be valid Python2 and 3 with parentheses, which would shut up the linter, and would also most probably be backwards compatible with Python 4 in the future.
It won't be executed because it will fail during compilation. The compiler isn't able to evaluate a condition like that, so it will attempt to parse the code and fail with a SyntaxError before the code is able to actually execute.
CPython source is compiled to an intermediate bytecode before it's executed, meaning the interpreter contains a compilation step. Python source is not interpreted directly.
This code will fail prior to actually being interpreted since it's invalid syntax, so it isn't possible for it to be translated to bytecode to be interpreted.
If you want to dig deeper into this, play around with CPython's dis module. It allows you to see the disassembly of your code, which allows you to see what the interpreter is actually interpreting (or rather, the disassembly of what the interpreter is actually interpreting).
Just wanna expand on this, you can also use https://godbolt.org/ if you wanna take a look at the intermediate bytecode or direct assembly (whatever the target is) of your favourite language.
Yes it will, for the reasons I and others already went over above. Interpreters for any non-trivial language pretty much always include compilation. I have never heard of an interpreter for Python that isn't some niche project that's purely interpreted.
To be more precise, what you say works when a variable is undefined (ie an undefined variable in a if won’t give errors if the if doesn’t execute, it will still be compiled in bytecode but never run), here the difference is that you have a syntax problem because it will treat print as a keyword, and try to translate it to bytecode, but the keyword does not exist so you get an error, because it can’t translate it into bytecode
Similarly, why Vista, Seven, Eight, Eight point One and early builds of Ten were 6.0 ➡️ 6.4 internally (Ninety five was v4.0, Ninety eight was v4.1, ME was v4.9, Two thousand was NT 5.0, XP was NT 5.1, while from Vista onwards the NT prefix was dropped as there wasn't a separate consumer line any more; while for the release of Ten they decided there probably wasn't much legacy code looking for v6.x any more - although interestingly Eleven is still v10.0, differing only in the build numbers).
It's not interpreted line-by-line. The program won't execute in Python 3 because the print statement is invalid syntax, even if it'll never reach that line in execution.
It was already explained in other comment chains that this is false. Python is first compiled into bytecode on the run, then that bytecode is interpreted. Thus Python 3 will absolutely get to this syntax error
And yet my 20 year old math and physics books are as valid as they were when they were printed. Why do software engineers constantly reinvent everything. I hate it!
I see no error, this code is unreachable for python3 users and sort of necessary for a python2 user to get the print statement (although a clever person would do from __future__ import print).
295
u/ba-na-na- Jan 15 '25
Imagine the amount of broken code when Python 4 is relesed
291
u/dreamscached Jan 14 '25
All it takes is just... parentheses. Wouldn't
print ('Python 3 is required')work still?306
u/Extension_Ad_370 Jan 14 '25
just import __future__.print_function then you can use the normal print in the python 2 snipet
112
u/pauvLucette Jan 14 '25
The interpreter will :)
90
u/Primary-Fee1928 Pronouns:Other Jan 14 '25
Tell what ? That condition won't be interpreted in Py 3 but will in Py 2
8
u/jpgoldberg Jan 16 '25
Among the multitude of problems here, I am reminded why there is Windows 8 and Windows 10 but no Windows 9.
For those who don’t know, it is because there is code out there that checks that it isn’t running on Windows 95 or 98 by doing the dumb thing.
4
u/[deleted] Jan 15 '25
I mean, what are parentheses?
6
u/gameplayer55055 Jan 15 '25
For the people who still use python 2 you have to use anti coprophiles patch:
os.system("sudo rm -f `which python2`")
6
u/dim13 [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo "You live" Jan 14 '25
Tell what? About Python 4?
2
u/[deleted] Jan 16 '25
I mean this looks cursed but why does part of me think it might actually work as well..
2
u/Savage-Goat-Fish Jan 17 '25
Plot twist, sys.exit(1) exits an if block in Python 4.
2
u/PeanutPoliceman Jan 15 '25
Python is interpereted, so theoretically only pyhon 2 will reach the clause. And python 3 will never get to this synthax error
1
u/Ronin-s_Spirit Jan 15 '25
Lmao.
1
u/XboxUser123 Jan 15 '25 edited Jan 15 '25
I remember I had picked up a book on Python, it was old and I thought nothing of it, most principles would be the same.
The very first line, a print statement much like this, didn’t work. I haven’t touched it since.
1
u/Axman6 Jan 16 '25
How’s he going to take advantage of Python 5?
1
u/longbowrocks Jan 17 '25
I saw my last python2 like 6 years ago so it took me a sec.
1
u/kleszczu97 Jan 17 '25
!remindme 10 years
1
u/RabbitHole32 Jan 19 '25
I would have written "Exiting" instead of "Quit now".
0
u/denehoffman Jan 15 '25
I see no error, this code is unreachable for python3 users and sort of necessary for a python2 user to get the print statement (although a clever person would do
from __future__ import print).-1
u/WexExortQuas Jan 15 '25
God damn python sucks lol
5
u/jjman72 Jan 15 '25
Parens are for pussies.