Some Python Design Patterns

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…

Advertisements

Tags: , ,

8 Responses to “Some Python Design Patterns”

  1. Pablo2M Says:

    This line has a error, the correct line is
    b = {“apple”: “johny”, “dog”: “janet”, “rose”: “Hannah”}[a]

  2. tolomea Says:

    You have a bad copy paste error in the second python snippet

  3. Vitalik Buterin Says:

    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]();

    • tolomea Says:

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

    • christopherolah Says:

      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.

  4. Vitalik Buterin Says:

    Sorry about the bad formatting on my above comment – wrote it from a cellphone. You get the idea though.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: