Tuesday, November 18, 2008

What Python learned from economics

I find economics to be a fairly interesting subject, mind you I'm bored out of my mind about hearing about the stock markets, derivatives, and whatever else is on CNBC, but I find what guys like Steven Levitt and Steve E. Landsburg do to be fascinating. A lot of what they write about is why people do what they do, and how to incentivise people to do the right thing. Yesterday I was reading through David Goodger's Code Like a Pythonista when I got to this portion:

LUKE: Is from module import * better than explicit imports?

YODA: No, not better. Quicker, easier, more seductive.

LUKE: But how will I know why explicit imports are better than the wild-card form?

YODA: Know you will when your code you try to read six months from now.

And I realized that Python had learned a lot from these economists.

It's often dificult for a programmer to see the advantage of doing something the right way, which will be benneficial in six months, over just getting something done now. However, Python enforces doing things the right way, and when doing things the right way is just as easy as doing in the wrong way, you make the intuitive decision of doing things the right way. Almost every code base I've worked with(outside of Python) had some basic indentation rules that the code observed, Python just encodes this into the language, which requires all code to have a certain level of readability.

Django has also learned this lesson. For example, the template language flat out prevents you from putting your business logic inside of it without doing some real work, you don't want to do that work, so you do things the right way and put your business logic in your views. Another example would be database queries, in Django it would be harder to write a query that injected unescaped into your SQL than it would be do the right thing at use parameterized queries.

Ultimately, this is why I like Python. The belief that best practices shouldn't be optional, and that they shouldn't be difficult creates a community where you actively want to go and learn from people's code. Newcomers to the language aren't encouraged to "just get something working, and then clean it up later," the communiity encourages them to do it right in the first place, and save themselves the time later.


  1. While I'm glad you like it, I don't want to claim credit falsely. The Luke/Yoda exchange didn't originate with me, I just adapted it. Here's the original:


    I'll add that link to my text.

  2. view logic can be written in Python too, and a little bit of that in templates is OK. django templates are too restrictive in this regard.

  3. > However, Python enforces doing things the right way

    Sorry, I think it is common knowledge that Python will let you get away with all sorts of shenanigans if you want to. (There is passing reference to this in one of the Python O'Reilly books).

    Python is very hacker-friendly - please don't mistake top-notch syntax and good organization for imposing an ideological vision on users. For comparison, see this entry in the jargon file:


    Also, I wouldn't mention "best practices" in the same sentence as "Python" within spitting distance of c.l.p unless you want to risk getting slapped in the face with a wet fish.

  4. I personally like this Fibonacci number calculator, written in Python of course:

    fibonacci = lambda x:map(lambda o:(map(lambda c:map(lambda l:
    reduce(lambda x,o:x+o,o[:2]),o[2]+1]])),range(x)),o)[1],[[1,1,0]+

    I'm so glad Python forced me to write readable code! Just kidding :) After working in Perl for a summer, it's an absolute lifesaver to be able to count on the consistent readability of Python. Thanks for the article!

  5. I think a lot of the good you see in Python code comes from the the community. People (like Eric, above), know about the evils of unreadable code and when they write it, they usually say that it is bad.


Note: Only a member of this blog may post a comment.