Re: Closures and Partial Function Application
Travis Parks jehugalea...@gmail.com writes: I also like partial function application. What is the easiest way of achieving this in Python? Would it look something like this: def foo(x, y): return x + y xFoo = lambda y: foo(10, y) from functools import partial xfoo = partial(foo, 10) -- Piet van Oostrum p...@vanoostrum.org WWW: http://pietvanoostrum.com/ PGP key: [8DAE142BE17999C4] -- http://mail.python.org/mailman/listinfo/python-list
Closures and Partial Function Application
I was a little disappointed the other day when I realized that closures were read-only. I like to use closures quite a bit. Can someone explain why this limitation exists? Secondly, since I can cheat by wrapping the thing being closure-ified, how can I write a simple wrapper that has all the same members as the thing (decorator), that then applies them to the underlying thing? I also like partial function application. What is the easiest way of achieving this in Python? Would it look something like this: def foo(x, y): return x + y xFoo = lambda y: foo(10, y) -- http://mail.python.org/mailman/listinfo/python-list
Re: Closures and Partial Function Application
On 31 August 2011 17:45, Travis Parks jehugalea...@gmail.com wrote: I was a little disappointed the other day when I realized that closures were read-only. I like to use closures quite a bit. Can someone explain why this limitation exists? Secondly, since I can cheat by wrapping the thing being closure-ified, how can I write a simple wrapper that has all the same members as the thing (decorator), that then applies them to the underlying thing? I don't understand. Can you give an example? I also like partial function application. What is the easiest way of achieving this in Python? Would it look something like this: def foo(x, y): return x + y xFoo = lambda y: foo(10, y) from functools import partial foo10 = partial(foo, 10) HTH Arnaud -- http://mail.python.org/mailman/listinfo/python-list
Re: Closures and Partial Function Application
On Wed, Aug 31, 2011 at 9:45 AM, Travis Parks jehugalea...@gmail.com wrote: I was a little disappointed the other day when I realized that closures were read-only. I like to use closures quite a bit. Assuming I'm intuiting your question correctly, then you're incorrect; they are read/write. You just need a `nonlocal` declaration for the variables in question. See http://www.python.org/dev/peps/pep-3104/ and http://docs.python.org/release/3.1.3/reference/simple_stmts.html#nonlocal for details. Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: Closures and Partial Function Application
On Aug 31, 1:18 pm, Chris Rebert c...@rebertia.com wrote: On Wed, Aug 31, 2011 at 9:45 AM, Travis Parks jehugalea...@gmail.com wrote: I was a little disappointed the other day when I realized that closures were read-only. I like to use closures quite a bit. Assuming I'm intuiting your question correctly, then you're incorrect; they are read/write. You just need a `nonlocal` declaration for the variables in question. Seehttp://www.python.org/dev/peps/pep-3104/ andhttp://docs.python.org/release/3.1.3/reference/simple_stmts.html#nonl... for details. Cheers, Chris Cool. So I just need to put nonlocal in front of the variable name. -- http://mail.python.org/mailman/listinfo/python-list
Re: Closures and Partial Function Application
On Aug 31, 1:51 pm, Travis Parks jehugalea...@gmail.com wrote: On Aug 31, 1:18 pm, Chris Rebert c...@rebertia.com wrote: On Wed, Aug 31, 2011 at 9:45 AM, Travis Parks jehugalea...@gmail.com wrote: I was a little disappointed the other day when I realized that closures were read-only. I like to use closures quite a bit. Assuming I'm intuiting your question correctly, then you're incorrect; they are read/write. You just need a `nonlocal` declaration for the variables in question. Seehttp://www.python.org/dev/peps/pep-3104/ andhttp://docs.python.org/release/3.1.3/reference/simple_stmts.html#nonl... for details. Cheers, Chris Cool. So I just need to put nonlocal in front of the variable name. Am I doing something wrong, here? nonlocal isn't registering. Which version did this get incorporated? -- http://mail.python.org/mailman/listinfo/python-list
Re: Closures and Partial Function Application
On Wed, Aug 31, 2011 at 12:02 PM, Travis Parks jehugalea...@gmail.com wrote: Am I doing something wrong, here? nonlocal isn't registering. Which version did this get incorporated? 3.0 -- http://mail.python.org/mailman/listinfo/python-list
Re: Closures and Partial Function Application
On Aug 31, 2:18 pm, Ian Kelly ian.g.ke...@gmail.com wrote: On Wed, Aug 31, 2011 at 12:02 PM, Travis Parks jehugalea...@gmail.com wrote: Am I doing something wrong, here? nonlocal isn't registering. Which version did this get incorporated? 3.0 Ah, okay. It would be really useful for unit testing. Unfortunately, I want to make the code I am writing compatible with 2.x and 3.x. I will just deal with it until 3.x takes over. Glad to know Guido sees the importance. -- http://mail.python.org/mailman/listinfo/python-list
Re: Closures and Partial Function Application
On 31 août, 18:45, Travis Parks jehugalea...@gmail.com wrote: I was a little disappointed the other day when I realized that closures were read-only. I like to use closures quite a bit. They are not _strictly_ read only, but Python being first and foremost an OO language, it's usually way simpler to use OO instead of closures when you start needing such features. -- http://mail.python.org/mailman/listinfo/python-list
Re: Closures and Partial Function Application
On Aug 31, 2:03 pm, bruno.desthuilli...@gmail.com bruno.desthuilli...@gmail.com wrote: On 31 août, 18:45, Travis Parks jehugalea...@gmail.com wrote: I was a little disappointed the other day when I realized that closures were read-only. I like to use closures quite a bit. They are not _strictly_ read only, but Python being first and foremost an OO language, it's usually way simpler to use OO instead of closures when you start needing such features. I like to leave OO to large-scale architectures and leave functional paradigms for implementation details. Writing an entire class for wrapping an int seems excessive. Especially if that code is limited to a small scope. I agree, though, that there is a time and a place for everything. -- http://mail.python.org/mailman/listinfo/python-list
Re: Closures and Partial Function Application
On 8/31/2011 12:45 PM, Travis Parks wrote: I was a little disappointed the other day when I realized that closures were read-only. 'Were', in 2.x. The standard 2.x workaround for a single nonlocal is to wrap it in a list. def f(): i = [0] def g(): i[0] += 1 for j in range(5): g() print(i) f() # 5 -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list