It takes a long time to get to the point where you stop learning a language, perhaps you never do stop.

Some useful Python tidbits I’ve recently picked up that I feel I ought to have known much earlier:

Using the dict constructor instead of the literal syntax:

# awkward {'cht': 'lc', 'chs': '500x600'} # nice dict(cht='lc', chs='500x600')

This is mentioned in the tutorial. Bad code monk. (Ah, this is that new fangled Python 2.3; that explains why I previously didn’t know it. Basically I learnt the vast majority of my working Python when lambda was new and «from __future__ import nested_scopes» was necessary)

When I met Raymond Hettinger at PyCon UK (I was doing a “Code Clinic” with him) one of the first questions I asked him was “when are we going to get unzip “; when he replied “zip is also unzip, you can just use zip(*l) ” I had one of those forehead slapping moments (quite literally I think). I course I knew that “ zip(*l) ” transposed a matrix (see Norvig’s IAQ), but I wasn’t used to thinking of a list of pairs, say, as being a matrix that I could transpose to get a pair of lists. Kick! Punch! It’s all in the mind.

Using split instead of literal lists:

# awkward ['F', 'PD', 'AD', 'D', 'TD', 'ED', 'ABO'] # nice 'F PD AD D TD ED ABO'.split()

This isn’t so much a new trick, but I was always a bit embarrassed about it. But now I’ve seen other Python programmers do it too, so I know it’s more socially acceptable.

Now one that I don’t actually use so much, nicked from Thomas Guest’s blog:

"chd=s:%(xs)s,%(ys)s" % locals()

Can you see what’s going on? The variables xs and ys are local variables whose values are spread into the string using «% locals()» as a sort of “interpolate from local variables” operator. I’m not a great fan, but I can see that it is useful. I’m not such a fan because it uses locals which is very cool, but could interfere with a compiler’s optimisations. Though in this case, with a constant string on the left of the % operator, the compiler has enough information to do a good analysis.

Speaking of Python string formatting, I was disappointed to learn that the optimisation of compiling format strings, which is routine in the Lisp world, is not generally done in Python. The optimisation I am talking about is one where «”some constant string” % stuff» gets converted into «some_function(stuff)» where some_function is a compiled function that does the formatting. It’s just one of the signs of how immature whole Python thing is.

And one specially for Pythonistas doing Google charts:

>>> d = dict(cht='lc', chs='500x600') # The dict from above >>> '&'.join(map('='.join, d.items())) 'chs=500x600&cht=lc'

An earlier version of this example involved a lambda: «’&’.join(map(lambda item: ‘=’.join(item), d.items()))», but then just as I was pasting it into this article I realised I could drop the lambda altogether. Bound methods rule!

What Python have you learnt recently?

Share this: Twitter

Facebook

Like this: Like Loading... Related