Bondage and Discipline Python

Python has an identity crisis sometimes. It starts with the premise, from Guido’s prior work on ABC, to make a simple but easy to understand language.

But then turns around and cries out “one way to do it“, leaving the programmer perplexed as to how Guido van Rossum thought we should do things. For example, Guido hates tail calls, so recursion isn’t the one way to do it that he picked (note that his blog post and followups contain a large number of factual errors; read it as an opinion piece only).

During these fits, Python suffers itself to be a bondage and discipline language.

Apparently there is hope. One bit that causes me particular pain is that nested functions cannot rebind variables in the outer scope; only read from them. A case in point:

def f():
  x = 1
  def doubleIt():
    x *= 2 # local variable 'x' referenced before assignment
  doubleIt(); doubleIt();
  return x;

However, I was reading a piece on Factor, and it mentions that this restriction is lifted in Python 3.0. I’m still on 2.6 (the only differences I had been aware of were the somewhat arbitrary swapping of the ‘/’ and ” operators, and the insulting removal of map, filter, and reduce from Python 3.0). However, fixing this scoping rule, even if an extra keyword is needed, sure would be convenient for me.

Actually, there are a lot of improvements:

  • Various APIs returns views instead of mutable list (copies).
  • sorted is now built in. (I’ve had to write that myself so many times…)
  • Apparently map and filter are still here (though he still went and put reduce all the way off in functools. I guess you can’t have it all -_-)
  • Set literals, yay!

Now, if Guido would be so kind to add proper statement support to lambdas, I’ll switch ^_^. Oh, but Guido hates lambdas. Oh well, next time maybe.

— signed an ambivalent Python user

Join the Conversation


  1. Not sure what you mean by “proper statement support for Lambdas”. Lambda functions have been there in Python.

    1. Excellent question. Python lambdas lack any support for Python statements. For example, if one wants to conditionally execute code, they cannot use ‘if’ statements:

        (lambda x:
          if not x: print 0
            for y in x: 
              print x) # error

      Instead, you must use comprehensions to loop, short circuit operators to perform conditionals, etc:

        (lambda x:
          if not x else 
              for y in x)

      The latter is decidedly suboptimal.

Leave a comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.