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
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.


Popular posts from this blog

Ubuntu 20.04 on a 2015 15" MacBook Pro

I decided to give Ubuntu 20.04 a try on my 2015 15" MacBook Pro. I didn't actually install it; I just live booted from a USB thumb drive which was enough to try out everything I wanted. In summary, it's not perfect, and issues with my camera would prevent me from switching, but given the right hardware, I think it's a really viable option. The first thing I wanted to try was what would happen if I plugged in a non-HiDPI screen given that my laptop has a HiDPI screen. Without sub-pixel scaling, whatever scale rate I picked for one screen would apply to the other. However, once I turned on sub-pixel scaling, I was able to pick different scale rates for the internal and external displays. That looked ok. I tried plugging in and unplugging multiple times, and it didn't crash. I doubt it'd work with my Thunderbolt display at work, but it worked fine for my HDMI displays at home. I even plugged it into my TV, and it stuck to the 100% scaling I picked for the othe

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 B

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 , 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.&quo