[Numpy-discussion] Vectorizing a function
Charles R Harris
charlesr.harris@gmail....
Wed Jan 30 12:16:39 CST 2008
On Jan 30, 2008 10:18 AM, Timothy Hochberg <tim.hochberg@ieee.org> wrote:
>
>
> On Jan 30, 2008 10:10 AM, Charles R Harris <charlesr.harris@gmail.com>
> wrote:
>
> [SNIP]
>
>
> >
> > IIRC, the way to do closures in Python is something like
> >
> > In [5]: def factory(x) :
> > ...: def f() :
> > ...: print x
> > ...: f.x = x
> > ...: return f
> > ...:
> >
> > In [6]: f = factory("Hello world.")
> >
> > In [7]: f()
> > Hello world.
> >
> > There is a reason to do it that way, but I don't recall what it is.
> >
>
> You don't need the "f.x = x" line. It's possible it has some beneficial
> side effect that I don't recall, but basic closures work fine without out.
>
You're right. It looks like the context gets stored in f.func_closure.
In [14]: f.func_closure[0].cell_contents
Out[14]: 'Hello world.'
Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20080130/26d375ed/attachment.html
More information about the Numpy-discussion
mailing list