PyCon: Cooking with Python 3

Cooking with Python 3

Side note: I met a guy from Lockheed Martin who was working on the Joint Strike Fighter using C++. He said that "malloc" and "new" were not allowed; everything had to be statically allocated.

Python3 now uses "except ValueError as e:" instead of "except ValueError, e:".

There are a bunch of really subtle changes in Python3, such as the fact that some_exception[0] no longer works.

Python3 chains exception messages if an exception occurs while another exception is being handled.

If you write "[x * x for x in nums]; print x", x won't be in scope. This is a bit surprising.

The exception object does not escape the except block. It won't be defined outside the block. This is really surprising in situations like:
e = True
except Exception as e:
print e
It'll say that e isn't defined when you try to print it. It was flagged as a bug in the bug tracker, but then closed as invalid.

Exec has been locked down a lot in Python3. For instance, this no longer works:
def foo():
exec("a = 23")
execfile is gone.

reload has moved to the imp module.

There is now a new set literal notation, "fruit = {'apple', 'banana'}", although you can't use it to create an empty set for obvious reasons.

There are now set comprehensions: "upper_fruits = {f.upper() for f in fruits}".

There are now dict comprehensions: "d = {name.upper(): value for name, value in stock.items()}".

"some_dict.keys()" gives you a "view" on the dict, not a list.

"zip" now produces an iterator, not a list. This sort of thing is very common in Python3.

You can now write: "a, *middle_items, b = [1, 2, 3, 4]".

The "*" in the following forces all the subsequent arguments to be keyword arguments (meaning you must specify the keyword when passing them): "def foo(x, *, y)".

Function annotations are new. They look like "def add(x: int, y: int) -> int:...". By default, they don't actually do anything. However, libraries can create whatever meaning they want for them. The thing after the colon can be any Python expression. This adds an __annotations__ member to the function object.

You could easily build a function decorator that uses the annotations to do optional static type checking.

If multiple libraries use annotations for different things, you could end up with conflicting uses of function annotations.

Use StringIO for strings and BytesIO for bytes.

This is a byte object: b"byte".

argparse is a new argument parser descended from optparse.

The division operator no longer truncates. It gives you a float. If you want integer division, use "1 // 3".

Use "2to3 -w ." to port an entire directory to Python3.

"unicode" and b"bytes" are completely separate.

2to3 is not a magic bullet. It's actually pretty stupid.

The bytes vs. unicode thing is by far the hardest part of porting.

We tried porting redis-py to Python3. I now think that the unicode vs. bytes problem is going to making porting extremely difficult, and it's going to lead to a lot of bugs.

The following is False 'b"H"[0] == "H"'. That's because it's actually equal to 72. This is going to lead to a lot of subtle bugs during porting.

There is a unicodedata module to look up unicode character metadata.