Does anyone know of a quick guide to all the syntax changes by Python version?
I realize I could look through all the various “What’s New in Python 3.x” documents and eventually figure it out. Or at all the PEPs and which version they were accepted for.
But my hope is that someone wrote a blog post, or a page at python.org I’ve overlooked, that summarizes this in a single table, or with sections by version, or some other convenient form.
Note: For my current purpose, I’m interested in syntax changes per se, not in semantic changes or stdlib additons, etc.
That’s a nice document, but I’m more interested in e.g. a summary of which version added the walrus operator, which version added exception groups, which version added the switch statement
… or even like in a TV show I stopped watching a couple nights ago: “which version added context managers (with statement)?” I stopped watching when a character was portrayed as using with in 2003 :-). The lack of verisimilitude was painful to me.
Well, I only made it about 4 1/2 minutes into The Billion Dollar Code. I’m sure it gets worse later :-).
FWIW, my question isn’t because I want to rate TV shows as a Python old-timer.
I’ve started developing a FLOSS tool called Semantic Diff Tool (sdt) to hopefully aid development workflows (PR reviews, etc). One thing it will be able to do is utilize parse trees for different programming languages, but based on specific language versions.
It’s very, very early into development, but you are welcome to look at GitHub - atlantistechnology/sdt: Semantic comparison of versioned files with git awareness. The tool itself is written in Golang, but it makes external calls to however many languages I manage to add. That said, it’s really not a Python tool per se; it’s neither written in Python (other than one line calling python -m ast -a) nor restricted to analyzing Python. Still, that’s one of the first languages I added support for.
I reckon I should probably write such a summary of the Whats New summaries myself. Addressing only syntax shouldn’t require a long document (it’s not for the full BNF of each thing, just a one-sentence description of the overall change; not even the motivation for each).
So a question for folks here: how should I publish such a document, assuming I get around to writing it. I reckon it should be permanently accessible; i.e. not a post on a transient discussion forum. All the URLs at my gnosis.cx domain have remained immutable for 20+ years at this point (except when my web host badly messed that up 2 years ago, but it’s generally fixed), so I suppose that’s an option. But that’s hardly a site anyone would look for.
@erlendaasland If I find anything actually missing in What’s New docs, I’ll definitely PR a change. But I very much doubt I will.
Hmm, sounds like a job for the Python wiki. FrontPage - Python Wiki Make yourself an account there if you haven’t already, and hit us up on the pydotorg-www mailing list if you have any issues.
If you find anything actually missing in the What’s New doc (or in the docs in general), it is a documentation bug and should as such be fixed. It is super helpful if you file an issue bug tracker, and even better if you can provide a PR. You can ping me on GitHub (@erlend-aasland).
cc. @CAM-Gerlach who has been heavily involved with the 3.11 What’s New doc.
All the above is not encouraging to me. I am a beginner and am trying to find out why the following (an expert’s code) objects to the comma after Exceotions,
try: # try
import smbus # to import SMbus (a subset of I2C)
except Exception, err: # incorrect syntax at the comma ?
print err
pass
I’m trying to comment each line but “Python All in one for dummies” does not have such an example.
I don’t know what err is or where it is supposed to be.
Is the Python syntax continually changing so yesterday’s program will not run?
You’re observing changes in the except clause which were added for
the Python 3.x series (support for which was backported to some
later 2.x versions as well) circa 2006 with the introduction of PEP
3110: PEP 3110 – Catching Exceptions in Python 3000 | peps.python.org
So yes, there are sometimes backward-incompatible changes to the
interpreter, but in this case it looks like you’re trying to run
(very) old code written for Python 2.x with a recent 3.x interpreter
which does not recognize the syntax.
Yesterday’s program will almost always run. For the most part, changes to Python’s syntax are backward compatible - existing valid code remains valid - so the relevance of “which version introduced this syntax” is usually from the perspective of “if I use this syntax, how old a Python interpreter can I still run this on?”. For example, Python 3.8 introduced this syntax for function headers:
def spam(x, /, y):
...
which will not work on Python 3.7; but code written for 3.7 will still run on 3.8.
The one big exception is that code written for ancient versions of Python - versions older than 2.6, usually - will not always run on modern Pythons. What you’ve come across is a nasty bug magnet that got deprecated and finally removed in Python 3.0; the problem is that these two except statements behave very differently:
So the modern syntax except Exception as err: was introduced, and made available in parallel, and then finally the old syntax was removed.
Unfortunately, code this old is going to have other problems as well. I would recommend using the 2to3 modernizer tool to go through it and attempt to update it. On the plus side, Python 3.0 is about the only point where you’ll have that incompatibility, and that was released in 2008, so if you have any code written for a more modern version of Python, you will probably be fine.
Thanks for the replies.
I got this from GitHub dated 7 years ago which makes it useless to me as a beginner if it chokes a few lines in.
What exactly is intended by the comma and err?
except Exception, err:
I tried lookin it up in Python.org and get pages of stuff that is not relevant.
I understand that
except Exception:
means for an exception do the following. I don’t know what err refers to or how to correct the syntax.
I will look for the 2 to 3 modernizer tool.
Sorry, I totally confused which show I meant. Billion Dollar Code was in my “keep watching” queue, but the one with the 2003 Python on a computer screen was The Playlist (TV series) - Wikipedia. Sorry for that.