Python Gotchas

I’m currently required to use Python for COMP312. Having come from doing nothing but Haskell and Prolog in COMP304 you can imagine my mind isn’t exactly bent around Python’s way of doing things. Here are a couple of traps I ran into

First off is expressions and lambda

>>> def p(x): print(x)
...  
>>> f = lambda x:p(x) 
>>> f(5) 
5
>>> f = lambda x:print(x)
  File "<stdin>", line 1
    f = lambda x:print(x)
                     ^
SyntaxError: invalid syntax

You cannot put an expression in a lambda. What’s more there is no implicit return value in Python. There was a thread about this, for those of you with access to the MSCS forums. After that last post I wasn’t sure how to reply

Next up is scope

>>> def counter(x):
...     base = x
...     def inc():
...             oldbase = base
...             base = base + 1
...             return oldbase
...     return inc
... 
>>> c = counter(5)
>>> c()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 4, in inc
UnboundLocalError: local variable 'base' referenced before assignment

“Oh, but that should happen as base shouldn’t leak into inc!”

>>> def counter(x):
...     base = x
...     def inc():
...             oldbase = base
...             return base
...     return inc
... 
>>> c = counter(5)
>>> c()
5

Python is happy to read variables and mutate objects, but introducing assignment makes Python come up with a new variable for you. The obvious argument is that this counter should be a class. Perhaps true, but it still caught me out.


About this entry