Python Gotcha's?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat Apr 14 21:23:04 EDT 2012


On Sat, 14 Apr 2012 15:47:54 -0700, Bryan wrote:

> Miki Tebeka wrote:
>> If you have an interesting/common "Gotcha" (warts/dark corners ...)
>> please share.
> 
> Python 3(K) likes to use the same '.py' file extension as its
> incompatible predecessors, 

And so it should.

Python 2 and Python 3 are two dialects of the same language, with just a 
very few minor changes in syntax (although a fair number of changes in 
libraries). With a modicum of care, it is quite possible, even trivially 
easy, to write useful code that is compatible with both Python 2 and 3. 
Here's an example:

http://pypi.python.org/pypi/pyprimes


> and in some/many/most *nix implementations,
> it likes to install in the same place.

I won't speak for Unixes, but that is certainly not the case with Linux. 
Each Python version gets its own location:

[steve at ando ~]$ for vers in 2.4 2.5 2.6 3.2 ; do which python$vers ; done
/usr/bin/python2.4
/usr/local/bin/python2.5
/usr/local/bin/python2.6
/usr/local/bin/python3.2


The first, 2.4, is the system python; the others were installed by me. I 
didn't need to take any special effort to install to versioned locations, 
the make script does that by default. The only special effort needed was 
to run "make altinstall" instead of "make install" to ensure that 
"python" without a version number still points to the system python 
rather than the freshly installed version.


> Python 3 is an improvement upon
> Python 2, but Python went from, "sure... Python just works," to,
> "well... that depends... which Python?"

I think that's nonsense. There has never been a time that you didn't have 
to think about "which Python", except maybe the very first public 
release. Every version has had differences from previous versions -- they 
wouldn't be different versions otherwise. Bugs are fixed, but more 
importantly new features are added. Libraries are deprecated and then 
removed. If you write code using Python 2.4 features, it may not work 
with Python 2.3. If you write code using 2.5 features like ternary if 
expressions, it certainly will not work in Python 2.4.

The 2to3 transition isn't the only time that Python has made backwards 
incompatible changes. I recently had to upgrade some Python code using 
string exceptions from Python 2.3 to 2.6. That was tedious but easy. 
However getting the tkinter code to upgrade was exciting and difficult.

Another example: the semantics of hex() and oct() have changed, probably 
sometime around 2.4 or 2.5. A third: the introduction of nested scopes 
back in 2.1. This changed the behaviour of any code using nested 
functions, and was the inspiration for the introduction of __future__. A 
fourth: turning None into a keyword in version 2.4.

I don't intent to be rude, but anyone who isn't a complete newbie to 
programming but is surprised to the point of "gotcha" by version 
compatibilities simply hasn't been paying attention.


-- 
Steven



More information about the Python-list mailing list