Back in 2008, Christopher Lenz wrote a great article called The Truth About Unicode in Python, which covers a lot of useful information that you wouldn't get just by reading the Unicode HOWTO, and that you might not even know to ask about unless you were already… well, not a Unicode expert, but more than a novice.

Unfortunately, that information is out of date, and he hasn't updated it. So, I think it's time for a new version.

Read his article for the state of Python 2.5, then come back here for the differences in Python 3.4.
1

You've probably been told that you can convert any recursive function into an iterative loop just by using an explicit stack.
1

If you want to write a forking server, one obvious way to do it is to use the multiprocessing module to create a pool of processes, then hand off requests as you get them.

The problem is that handling a request usually involves reading from and writing to a client socket. So, you need to send the socket object as part of the request. And you can't pickle sockets.

I was recently trying to optimize some C code using cachegrind, and discovered that branch misprediction in an inner loop was the culprit. I began wondering how much anything similar could affect Python code.
3

A lot of people have questions like this:

I've got a 100MB CSV file. I read it in to a list, populated with a csv.DictReader, and my computer starts swapping. Why? Let's look at what it takes to store a 100MB file as a list of dicts of strings.

But first, let's look at how to solve this problem, instead of how to understand it.
1

There are hundreds of questions on StackOverflow from people who want to know something like, "How do I edit a file in-place, without creating a new file."

In general, the answer is, "You can't. You really do want to create a new file." (There is an exception, which I'll get to later.)

Why? Well, files are just streams of bytes—or, for text files, streams of characters.

Recently, as an aside to the proposal for static type annotations in Python, Guido offhandedly noted another suggested improvement to Python, adding ADTs (Algebraic Data Types). This has also come up in discussions about pattern matching, and about existing run-time and compile-time static-typing libraries.
5

The idea of a pattern-matching case statement has come up a few times recently (first in a proposal to add a C-style case statement, more recently as part of a the proposal to add static type annotations), but the discussion ends as soon as people realize that it's not quite as easy as it appears.

The core problem is that the key to pattern matching is unpacking constructors of algebraic data types, and Python has arbitrary OO-style classes, not ADTs.
2

Weak typing

A language with weak typing is one where programs can escape the type system. Another way to describe it is that values can change types.

For example, in C, I can create a pointer to characters, then tell the compiler I want to use it as a pointer to integers:

char sz[] = "abcdefg"; int *i = (int *)sz; On a little-endian platform with 32-bit integers, this makes i into an array of the numbers 0x64636261 and 0x00676665.

The official tutorial does a great job explaining list comprehensions, iterators, generators, and generator expressions at a high level. Since some people don't want to read even that much, I wrote a post on comprehensions for dummies to summarize it.

And if you want to know the lowest level nitty gritty of how they work, there's documentation to point you to the right place, and then the code is relatively readable, at least if you understand C well and know the basics of how CPython works.
1
Blog Archive
About Me
About Me
Loading
Dynamic Views theme. Powered by Blogger. Report Abuse.