Python 3

A couple of months ago I decided to switch to Python 3.1 for all my spare time projects. Previously I’d been using the Python which is shipped by default on my Mac, 2.6.1. I’m always keen to keep up with new things, and it seemed like maybe enough libraries had been ported to Python 3 to make shifting over easy.

The bytes/str change is the biggest win. In Python 3, a ‘str’ is a string; it’s text in some defined encoding. If you want to have binary data, you can – that’s ‘bytes’. You can go back and forth between the two, but you must provide an encoding when you do so. This is a huge win, as a ‘str’ is always safe to print. It can’t contain binary that isn’t valid according to your string encoding. A string with an umlaut in it is no longer going to unexpectedly break your code.

The removal of ‘lambda’ is a pain. Yes, we’ve got list comprehensions and generator expressions to do one liners, but I still liked ‘lambda’. Without it I find myself declaring a lot of small functions and then calling them just below their definition, which is less readable. [ Edited; it turns out (see comments) that this paragraph is completely wrong ]

Training my brain that print is a function not a statement has been surprisingly hard.

A real problem has been the lack of third party library support for Python 3. It’s nowhere near as far along as I’d thought. I’ve ended up porting Python bindings for things like CouchDB and Twitter to Python 3, but then I’m left with my own fork to maintain. Most of the work of the port is down with the impressive ‘2to3’ tool, but typically there’s some ‘str’ vs ‘bytes’ bits to fix up. Not very fun, and tedious as to try out a new third party library I have to do a bunch of work.

This last problem leads to my biggest problem; sharing my Python 3 programs with other people sucks. I’ve written a little command line twitter client, and to use it a person has to go download a couple of branches of third party libraries I’ve forked to add Python 3 support. A huge pain for that person, and a pain for me as I end up with no users and thus no contributors.

I’m thinking of going back to Python 2, or even shifting off Python to another more exciting language. Any comments from people?

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

6 Responses to Python 3

  1. David Adam says:

    lambda is gone?

    Python 3.1.3 (r313:86834, Nov 28 2010, 10:01:07)
    [GCC 4.4.5] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> x = lambda y: y
    >>> x(2)
    2

    • Reena says:

      I’m glad you were able to set up a new theme. Here’s the trade-off, and how it afcefts you. A child theme is best in my opinion, as in essence, it builds off of the parent theme. When the parent theme gets updated, you benefit from the updates. A whole new theme will no longer get any updates, so there will be no way for you to ‘update’ the theme. Basically what you would need to do is read any changelogs that come out if 2011 gets updated, to see what was updated and why, and manually copy the code to your theme, if you feel the changes are something you need. For instance if it’s some security fix, or bugfix or something.Now a child theme would have the same problem in a sense. If you get into heavily customizing a child theme, you get to the point where you may need to copy a file from the parent to the child. Obviously when the parent theme gets updated, it wouldn’t also update your child theme. And you couldn’t just copy the whole file over, as the reason you moved it to your child theme was to customize it. So again, it would me a manual inspection and copy of the code.When 2011 gets updated, a link is provided to trac. It’s a bit hard to figure out at first, but you can get in there and do a comparison on old/new files to see exactly what changed. It’s even highlighted so you don’t have to try to read entire files. Plus they give reasons. This will help you track down if you need to take any action.

  2. Um, Python 3 still has lambda. What made you think it was removed? Also, please use 3.2, which has been out for months now.

    • grahame says:

      Thanks for replying! I think I must have been tripped up by the syntax changes, and fooled by a memory of the discussion about removing it before 3.0.

    • grahame says:

      I’m using 3.2 on my Mac but 3.1 on my Debian machine, I’m running stable and trying to keep things working as easily as possible for someone running that distribution.

      I did end up trying to port my software back to Python 2 but then stopped. There’s too many useful changes in Python 3 to loose, so I’ve decided to be more persistent trying to get my branches of libraries with Python 3 support merged (or at least stick then on github/bitbucket and advertise them.)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s