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

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

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

Haskell or Erlang?

I've coded in both Erlang and Haskell. Erlang is practical, efficient, and useful. It's got a wonderful niche in the distributed world, and it has some real success stories such as CouchDB and jabber.org. Haskell is elegant and beautiful. It's been successful in various programming language competitions. I have some experience in both, but I'm thinking it's time to really commit to learning one of them on a professional level. They both have good books out now, and it's probably time I read one of those books cover to cover. My question is which? Back in 2000, Perl had established a real niche for systems administration, CGI, and text processing. The syntax wasn't exactly beautiful (unless you're into that sort of thing), but it was popular and mature. Python hadn't really become popular, nor did it really have a strong niche (at least as far as I could see). I went with Python because of its elegance, but since then, I've coded both p