Friday, October 13, 2006

Python: Modifying a Counter in a Closure

I helped a fellow Pythonista with a problem today, and it reminded me that a lot of people might not know about the trick of using a list to box variables within a closure. If that doesn't make sense, read the code below:
def counter(start):

"""Create a counter starting at ``start``."""

# The value "curr" needs to be wrapped in a list. Otherwise, when
# "+=" is used in "inc", Python binds the variable at that scope
# instead of at this scope. We don't want to redefine a variable at
# the inner scope. We want to modify the variable at this outer
# scope. Java programmers would call this "boxing", but they would
# use an Integer instance instead of a list.

curr = [start]

def inc():
"""Increment the counter and return the new value."""
curr[0] += 1
return curr[0]

return inc


if __name__ == '__main__':
c = counter(5)
for i in range(3):
print c() # Prints 6 7 8.

16 comments:

PJE said...

There's a better way, from a readability point of view. Replace "curr = [start]" with "inc.count = start", and move it after the "def inc():" block. Now replace all uses of "curr[0]" with "inc.count".

Now your code will be understandable even to the uninitiated. :)

Shannon -jj Behrens said...

Nice :)

Daniel said...

I've been using that array idiom for a while now but it always seemed a bit icky to me. Never thought of using a function attribute to do the job - it looks a lot nicer.

Anonymous said...

def inc(count=[0]):
count[0] += 1
return count[0]

Manuzhai said...

I'm kind of surprised that PJE's trick works... How is it that there's a variable "inc" bound to the function within the function itself? Apparently, that would be always the case, but I don't see how that makes sense. It's impossible to declare something in its OWN scope, right?

Justin said...

I don't get it. What makes this superior to:

def counter(start, stop):
while start < stop:
yield start
start += 1

for i in counter(5,10):
print i

# prints 5 6 7 8 9

or even

for i in [i + 5 for i in range(3)]:
print i
# prints 5 6 7

?

Anonymous said...

"How is it that there's a variable "inc" bound to the function within the function itself?"

"inc.count" is an attribute on the function object, not a variable inside the function.

alexmdac said...

You can use this trick to translate the established technique for making private data in Javascript into Python.

class Foo:
  def __init__(self):
     private_vars = [ 0, "Alex" ]

     def count(): return private_vars[0]
     self.count = count

     def set_count(c): private_vars[0] = c
     self.set_count = set_count

     def name(): return private_vars[1]
     self.name = name

     def set_name(n): private_vars[1] = n
     self.set_name = set_name

Now you can do this:

f = Foo()
f.set_name("someone else...")

Shannon -jj Behrens said...

> def inc(count=[0]):
> count[0] += 1
> return count[0]

Unfortunately, this provides only a single global counter, which is insufficient. The other versions create a new counter for each closure.

Mutable static defaults--ugly ;)

Shannon -jj Behrens said...

> I'm kind of surprised that PJE's trick works...

Functions are objects. With the exception of some immutable objects like strings, you can attach stuff to any object.

Shannon -jj Behrens said...

> I don't get it. What makes this superior to:

Sorry, you missed my point. You are absolutely right that generators are better mechanisms to create counters. I wasn't really trying to create a counter. I was trying to show how to modify a lexically scoped, non-local variable using the list trick.

Shannon -jj Behrens said...

> You can use this trick to translate the established technique for making private data in Javascript into Python.

Nice! I hadn't seen that :)

Manuzhai said...

> Functions are objects. With the exception of some immutable objects like strings, you can attach stuff to any object.

I understand that I can attach attributes to any function. What I don't understand is that the function object is available in the function body when the function evaluation hasn't been completed yet (in the function body).

Shannon -jj Behrens said...

> I understand that I can attach attributes to any function. What I don't understand is that the function object is available in the function body when the function evaluation hasn't been completed yet (in the function body).

Oh, now I see your confusion. You are correct that you cannot attach function attributes (that I know of) before the function definition is done. PJE was saying you can attach attributes to the inner function from the outer function.

Manuzhai said...

But that is EXACTLY what he's saying: "replace all uses of 'curr[0]' with 'inc.count'. The uses of curr[0] are all *inside* inc(). And it works, too, I tested it in the interactive interpreter:

>>> def inc():
... inc.count += 1
... return inc.count
...
>>> inc.count = 0
>>> inc()
1
>>> inc()
2

Hmm, that makes sense, actually. By the time the function body is executed, the inc() function is fully defined in the global namespace, so now the function body can access the link to the function object in the global scope.

Shannon -jj Behrens said...

> Hmm, that makes sense, actually.

Ah, yes, you are correct. Very good. It's not really "global" of course, but I think you've nailed it.