Python 3.0, first impressions
For one of my new personal projects, I decided to use Python 3.0. I want to become more familiar with Py3K, and it’s not like this project will be useful to (m)any people anyway, so this is a good time to experiment.
Here are some of my findings. Most of these I had read about before, but I didn’t remember them until I tried writing code 2.x-style.
- I intuitively write “print blah-de-blah“, without parentheses, which doesn’t fly anymore as print is a function in Python 3.0. This has been biting me surprisingly often. I figure it will keep running into this every now and then, as long as I still use both 2.x and 3.0.
- Tuple parameter unpacking has been removed (PEP 3113), and I just happened to run into a case where this would have been useful. :-) No big deal though.
- raw_input has been renamed to input.
- I was somewhat surprised to find that import string still works… unfortunately, string.join is gone, so I have no choice to but to use str.join. *grumbles*
- map and friends now return an iterator rather than a list. This one bit me when I created a list (or so I thought) at the toplevel, then tried to search it multiple times from inside a function. Although I read about it, I didn’t see this one coming, as I’ve been tinkering with functional languages lately, and using map tends to produce a list there. Fortunately it’s easily fixed with a listcomp.
So far the only third-party library I have used is Nose. There is a Py3K branch for it, get it at:
svn co http://python-nose.googlecode.com/svn/branches/py3k nose-3k
As far as I can tell, it works fine; just use python3.0 setup.py install.
More later, as I hack some more on my stupid project. I miss IPython; maybe there’s a 3.0 branch for it too? Will have to find out. [Update: Apparently not.]
Michael said,
January 23, 2009 @ 2:00 pm
“Fortunately it’s easily fixed with a listcomp.”
If you need a list from an iterator, just use list(iterator).
Hans Nowak said,
January 23, 2009 @ 2:04 pm
Of course, but in this case my code looked like ‘list(map(lambda: …’ and I figured it would be clearer to use a list comprehension.
Greg said,
January 23, 2009 @ 2:42 pm
I’m pretty much worthless if I don’t have IPython. I just never learned to navigate or do much of anything in the standard python shell. IPython is great. I wish it were the default.