Skip to main content

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
try:
...
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")
print(a)
foo()
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.

Comments

Popular posts from this blog

Drawing Sierpinski's Triangle in Minecraft Using Python

In his keynote at PyCon, Eben Upton, the Executive Director of the Rasberry Pi Foundation, mentioned that not only has Minecraft been ported to the Rasberry Pi, but you can even control it with Python. Since four of my kids are avid Minecraft fans, I figured this might be a good time to teach them to program using Python. So I started yesterday with the goal of programming something cool for Minecraft and then showing it off at the San Francisco Python Meetup in the evening.

The first problem that I faced was that I didn't have a Rasberry Pi. You can't hack Minecraft by just installing the Minecraft client. Speaking of which, I didn't have the Minecraft client installed either ;) My kids always play it on their Nexus 7s. I found an open source Minecraft server called Bukkit that "provides the means to extend the popular Minecraft multiplayer server." Then I found a plugin called RaspberryJuice that implements a subset of the Minecraft Pi modding API for Bukkit s…

Creating Windows 10 Boot Media for a Lenovo Thinkpad T410 Using Only a Mac and a Linux Machine

TL;DR: Giovanni and I struggled trying to get Windows 10 installed on the Lenovo Thinkpad T410. We struggled a lot trying to create the installation media because we only had a Mac and a Linux machine to work with. Everytime we tried to boot the USB thumb drive, it just showed us a blinking cursor. At the end, we finally realized that Windows 10 wasn't supported on this laptop :-/I've heard that it took Thomas Edison 100 tries to figure out the right material to use as a lightbulb filament. Well, I'm no Thomas Edison, but I thought it might be noteworthy to document our attempts at getting it to boot off a USB thumb drive:Download the ISO. Attempt 1: Use Etcher. Etcher says it doesn't work for Windows. Attempt 2: Use Boot Camp Assistant. It doesn't have that feature anymore. Attempt 3: Use Disk Utility on a Mac. Erase a USB thumb drive: Format: ExFAT Scheme: GUID Partition Map Mount the ISO. Copy everything from the I…

ERNOS: Erlang Networked Operating System

I've been reading Dreaming in Code lately, and I really like it. If you're not a dreamer, you may safely skip the rest of this post ;)

In Chapter 10, "Engineers and Artists", Alan Kay, John Backus, and Jaron Lanier really got me thinking. I've also been thinking a lot about Minix 3, Erlang, and the original Lisp machine. The ideas are beginning to synthesize into something cohesive--more than just the sum of their parts.

Now, I'm sure that many of these ideas have already been envisioned within Tunes.org, LLVM, Microsoft's Singularity project, or in some other place that I haven't managed to discover or fully read, but I'm going to blog them anyway.

Rather than wax philosophical, let me just dump out some ideas:Start with Minix 3. It's a new microkernel, and it's meant for real use, unlike the original Minix. "This new OS is extremely small, with the part that runs in kernel mode under 4000 lines of executable code." I bet it&…