From: Keith Gaughan Date: 2006-07-27T09:09:43+09:00 Subject: Re: For performance, write it in C On Thu, Jul 27, 2006 at 08:53:18AM +0900, Keith Gaughan wrote: > On Thu, Jul 27, 2006 at 08:33:31AM +0900, Chad Perrin wrote: > > > Is it just me, or are there no proper closures in that example code? > > No, Chad. There's closures in there. What you're not seeing is > anonymous functions, but closures are not the same as anonymous > functions. Clarification: like Java, Python won't let you assign to variables in the outer scope, so you have to use an array or some other object to hack around that if you need that functionality. I know, it sucks, but the fact it doesn't allow you to assign to an outer scope doesn't stop Python from having closures, just that it doesn't trust the developer not to screw things up. Here's a better example: def foo(): x = [0] def bar(): x[0] += 1 print x[0] return bar baz = foo() baz() -> 1 baz() -> 2 baz() -> 3 Of course, this is better implemented as a generator. -- Keith Gaughan - kmgaughan@eircom.net - http://talideon.com/ Life is like a tin of sardines. We're, all of us, looking for the key. -- Beyond the Fringe