Python 3

I attended the Detroit PythonPerl Mongers meeting last night. They opened it up some time ago to include all the interpreted languages (Lisp, Python, etc), and so the lion’s share of last night’s meeting was about the brand new backwards-incompatible Python 3.0, and how it’s different from Python 2.x. I was asked via Identica to take notes, so here they are:

The presentation was given by Steven Kryskalla, who has more python stuff on his home page, but it seems a bit out of date (May 2007?). There’s slides there for what’s new in Python 2.5 (current versions being 2.6 and 3.0), but nothing from last night. At least not yet.

Mind you, I’m normally a horrible note taker. If you find these notes lacking, I should tell you that these are possibly the best notes I’ve ever taken, so be happy with what you got. Shoulda been at the meeting! 🙂

A link to the official What’s new?

Print is now a function. Primarily, this means extra worthless keystrokes, since you now have to enclose what you want printed inside parens. There’s also advantages to being able to call print as a function. For starters, you can now specify the End Of Line character to be something other than the default \n:
print(“hello world”, end=”*”)
That prints “hello world*” and does not give a newline. You can also change the separator from a space to something else, or specify a file to print to.
(attempting to declare a “sep” today, as I have in my notes, does nothing. Not even an error.)

All strings are unicode.

Unless you don’t want them to be. In which case you can use one of the two new datatypes, byte and bytearray

Iterators use lazy evaluation by default. The example of this is “range(9)”. Rather than printing out “0, 1, 2, 3, 4, 5, 6, 7, 8, 9”, it gives the result “range(0, 9)”. Supposedly this saves on memory by not evaluating until necessary. I merely find it annoying.

True division replaces int division. Thankyouthankyouthankyou! I have really hated the Python convention that 1/2=0. Now at long last 1/2=0.5

There is no more “long” datatype.

Can now use hex, octal, and binary literals (0x11, 0o11, 0b11, evaluating to 17, 9, and 3 in decimal). I really like this.

Functions can now have annotated variables, such as:
def f (x: int, y: “Some other annotation”):
I can see the annotations on this function with “f.__annotations__”. It’s important to note that Python doesn’t enforce variable typing via annotations. Even though I declared “x: int” in the function, I can pass a string to x and it still processes just fine.

Extended unpacking(a,*b,c = array… b gets multiple values). In other words:
a,*b,c = range(9)
a=0
b=1,2,3,4,5,6,7
c=8
a,b,*c = range(9)
a=0
b=1
c=2,3,4,5,6,7,8

New string formatting. He did some stuff to make the words in a short sentence come out in a totally different order, and showed how it was different from the old printf style of doing so. I didn’t pay very close attention to this.

I know a bunch of my Identica peeps use Django, so I wanted to link to the Porting Django to Py3k wiki.

Variants of Python (Jython/IronPython/etc.) don’t exist yet for 3.0

Last but certainly not least, there’s a rather nifty tool to help port Py2 code to Py3. It’s called, simply, “2to3”, and if you feed it a python file, it poops out a diff. Does a lot of the grunt work of upgrading (i.e. adding parens around the object of a print statement).

So that’s that. If these notes confuse the hell out of you, don’t say I didn’t warn you.

Advertisements
Post a comment or leave a trackback: Trackback URL.

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

%d bloggers like this: