With a summer job doing DNSSEC stuff in Python (for Xelerance) — and recently, web development — I’ve written a lot of python code in the last few weeks. And I’ve noticed that my code has developed a few short, retrospectively obvious, patterns that have simplified my code.
Simple Cases/Permutations with dicts
Have you ever written code of the form:
if a == "apple":
b = "johny"
elif a == "dog":
b = "janet"
elif a == "rose":
b = "Hannah"
...
This code, while simple, is tedious and space consuming. Here’s an alternative:
b = {"apple": "johny", "dog": "janet", "rose": "Hannah"}[a]
If you want an else case, just use a try…except statement.
I know some people may feel that the other way is clearer (Python seems to have an anti-dense code streak in its community), but I actually think that this way is conceptually simpler.
Replacing “== or ==” with “in List”
Instead of writing
if x == 1 or x == 2 or x == 3:
I now write
if x in [1, 2, 3]:
Use Python Iterators
I thought that in Python everyone would use python iterators. Then I started using ldns and learnt that I was wrong. Shortly after, I started writing ldnsx (Yay cool work!).
Provide Filters in Your APIs
If you’re returning a list of things, it is super handy to provide a filter, like I did in ldnsx or django does.
And now I’m out of ideas. But there’s a few things…
July 29, 2011 at 00:53 |
This line has a error, the correct line is
b = {“apple”: “johny”, “dog”: “janet”, “rose”: “Hannah”}[a]
July 29, 2011 at 04:18 |
Agh, silly me. Thanks!
July 29, 2011 at 02:07 |
You have a bad copy paste error in the second python snippet
July 29, 2011 at 04:19 |
Thanks!
August 6, 2011 at 00:24 |
You could combine the dicts with anonymous functions – replace:
if (x == 0) do_this();
elif (x == 1337) do_that();
elif (x == ‘horse’) do_something_else();
With:
{0:do_this,1337:do_that,’horse’:do_something_else}[x]();tions – replace:
if (x == 0) do_this();
elif (x == 1337) do_that();
elif (x == ‘horse’) do_something_else();
With:
{0:do_this,1337:do_that,’horse’:do_something_else}[x]();
August 6, 2011 at 01:42 |
> You could combine the dicts with anonymous functions
Or not.
When you are tempted to do something like this you should stop and ask yourself if you are making the program easier or harder to read. The purpose and behavior of the if elif else construct is obvious, with the dictionary version the reader has to stop and think about what it does.
August 6, 2011 at 03:26 |
I’ve thought about doing this before. It’s tempting. I’ve even toyed around with rewriting some segments of code this way. But all the ones I’ve tried have come out as uglier and… unpythonistic.
If I was writing in a language like haskell, where anonymous functions are more syntactically accessible and the culture values shorter more thoughtful code, I’d definitely do something like this. But then, in Haskell, I’d have a case statement that does the same thing, even more elegantly.
August 6, 2011 at 00:25 |
Sorry about the bad formatting on my above comment – wrote it from a cellphone. You get the idea though.