// I need to

ModernizeUpgrade without breaking it RescueFix what someone else started Build NewTurn an idea into a product Reach More PeopleGet found, get used, include everyone

// Company

Case StudiesExpertiseAbout + CareersResourcesContact Book a call →
≡articleBuild New

Python Gotchas

The two classic Python gotchas: mutable default arguments evaluated at definition time, and loop variables bound late in functions and lambdas.

Art+Logic Apr 12, 20132 min read

From the Art+Logic archive · written in 2013. The tools may have moved on; the thinking still holds.

There are only 2 gotchas in Python. The stack overflow post lists them, but also a bunch of other stuff. I ran into both of the gotchas in the space of a week recently and spent some time trying to figure out where the bug was. Here they are in case you find yourself running into them too.

1. Default arguments are evaluated when the function is defined.

Which seems reasonable. Except it leads to things that you created at different times saying they were created at the same time:

from datetime import datetime
import time
class Thing(object):
    def __init__(self, createdDate=datetime.now()):
        self.createdDate = createdDate
a = Thing()
time.sleep(1)
b = Thing()
print a.createdDate
print b.createdDate
## 2013-04-08 08:52:31.658985
## 2013-04-08 08:52:31.658985

and nodes all having the same children (because they have the same list instance in self.children):

class Node(object):
    def __init__(self, name, children=[]):
        self.name = name
        self.children = children
    def __repr__(self):
        return self.name
a = Node('parent')
b = Node('child')
a.children.append(b)
print a.children
## [child]
print b.children
## [child]

The typical pattern for not running into this is to have empty default args be None, and then check for None:

class Node(object):
    def __init__(self, name, children=None):
        self.name = name
        self.children = children
        if self.children is None:
            self.children = []
    def __repr__(self):
        return self.name
a = Node('parent')
b = Node('child')
a.children.append(b)
print a.children
## [child]
print b.children
## []

2. Variables in loops and functions or lambas are bound by name.

If you want to make functions in a loop, you have to be careful about how you write the function. If you use a variable from the loop in your function it will have the value of the last thing it gets set to.

ns = []
for fruit in ['apple', 'banana', 'orange']:
    def aFunction():
        print fruit
    functions.append(aFunction)
for fn in functions:
    fn()
## orange
## orange
## orange
lambdas = [lambda: x for x in [1, 2, 3]]
for fn in lambdas:
    print fn()
## 3
## 3
## 3
x = 57
for fn in lambdas:
    print fn()
## 57
## 57
## 57

Which is another way of saying that if you use an outside variable in a function when you call the function it’ll look up whatever the value of the variable is now, and use that. Which isn’t so surprising:

what = 'hello'
def sayWhat():
    print what
sayWhat()
## hello
what = 'how are you?'
sayWhat()
## how are you?
what = 'toodle pip'
sayWhat()
## toodle pip

And of course setting a variable in a loop changes whatever that variable was before:

x = 27
for x in range(3):
    pass
print x
## 2

so maybe this isn’t really a gotcha so much. You can work around this with default arguments (which are as you may have heard evaluated at function definition time):

lambdas = [lambda myNum=i: myNum for i in [1, 7, 13]]
for l in lambdas:
    print l()
## 1
## 7
## 13

So:

Default arguments are evaluated when the the function definition is evaluated. Not when the function is run.

and

A function using a variable from an outer scope will look up what the value of that variable is when the function is run. Which might catch you off guard if you’re creating functions in loops.

keep going

More to explore in Build New.

All Build New resources →
let's talk

Got a problem like this one?

Tell us what's stuck. A real person replies within one business day.

Talk to our team →

Talk to people who know software, not a sales script.

Your first call is with Aja, our VP of Sales. He'll help make sure you're solving the right problem. Here's what you'll cover together:

  1. What you're trying to accomplish

    The business goal first, then the technology.

  2. Possible approaches

    And the tradeoffs of each, in plain language.

  3. Risks you may not have considered

    Technical and business, before they cost you.

  4. Whether AI changes the math

    What's practical, and what's just hype for your project.

  5. Sensible next steps

    Even if the right next step is not us.

book-a-call.ics

Loading the scheduler…

Open the booking page →

Some problems are easier to explain in writing. Send us a few lines about what you're working on, and we'll reply within one business day. Helpful to include:

  1. What the software does, or should do
  2. What's stuck, or what's worrying you
  3. Any deadline or budget you have in mind
note.md

Loading the form…