From: Chad Perrin Date: 2006-07-27T11:28:20+09:00 Subject: Re: For performance, write it in C On Thu, Jul 27, 2006 at 09:09:43AM +0900, Keith Gaughan wrote: > 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 That's a bit clearer -- and it does look like a proper closure. It also looks unnecessarily complex to implement. Thanks for the example. -- CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ] "The ability to quote is a serviceable substitute for wit." - W. Somerset Maugham