Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #12499 > unrolled thread

Closures and Partial Function Application

Started byTravis Parks <jehugaleahsa@gmail.com>
First post2011-08-31 09:45 -0700
Last post2011-09-05 16:23 +0200
Articles 11 — 7 participants

Back to article view | Back to comp.lang.python


Contents

  Closures and Partial Function Application Travis Parks <jehugaleahsa@gmail.com> - 2011-08-31 09:45 -0700
    Re: Closures and Partial Function Application Arnaud Delobelle <arnodel@gmail.com> - 2011-08-31 17:55 +0100
    Re: Closures and Partial Function Application Chris Rebert <clp2@rebertia.com> - 2011-08-31 10:18 -0700
      Re: Closures and Partial Function Application Travis Parks <jehugaleahsa@gmail.com> - 2011-08-31 10:51 -0700
        Re: Closures and Partial Function Application Travis Parks <jehugaleahsa@gmail.com> - 2011-08-31 11:02 -0700
          Re: Closures and Partial Function Application Ian Kelly <ian.g.kelly@gmail.com> - 2011-08-31 12:18 -0600
            Re: Closures and Partial Function Application Travis Parks <jehugaleahsa@gmail.com> - 2011-08-31 11:26 -0700
    Re: Closures and Partial Function Application "bruno.desthuilliers@gmail.com" <bruno.desthuilliers@gmail.com> - 2011-08-31 11:03 -0700
      Re: Closures and Partial Function Application Travis Parks <jehugaleahsa@gmail.com> - 2011-08-31 12:33 -0700
    Re: Closures and Partial Function Application Terry Reedy <tjreedy@udel.edu> - 2011-08-31 18:21 -0400
    Re: Closures and Partial Function Application Piet van Oostrum <piet@vanoostrum.org> - 2011-09-05 16:23 +0200

#12499 — Closures and Partial Function Application

FromTravis Parks <jehugaleahsa@gmail.com>
Date2011-08-31 09:45 -0700
SubjectClosures and Partial Function Application
Message-ID<9cd48486-acd9-4888-9677-0e54fd1eedfd@k15g2000yqd.googlegroups.com>
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)

[toc] | [next] | [standalone]


#12500

FromArnaud Delobelle <arnodel@gmail.com>
Date2011-08-31 17:55 +0100
Message-ID<mailman.616.1314809735.27778.python-list@python.org>
In reply to#12499
On 31 August 2011 17:45, Travis Parks <jehugaleahsa@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

[toc] | [prev] | [next] | [standalone]


#12505

FromChris Rebert <clp2@rebertia.com>
Date2011-08-31 10:18 -0700
Message-ID<mailman.621.1314811146.27778.python-list@python.org>
In reply to#12499
On Wed, Aug 31, 2011 at 9:45 AM, Travis Parks <jehugaleahsa@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

[toc] | [prev] | [next] | [standalone]


#12510

FromTravis Parks <jehugaleahsa@gmail.com>
Date2011-08-31 10:51 -0700
Message-ID<426395c1-f4ec-4ba2-ad3e-8aaa52148c55@z15g2000yqk.googlegroups.com>
In reply to#12505
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.

[toc] | [prev] | [next] | [standalone]


#12511

FromTravis Parks <jehugaleahsa@gmail.com>
Date2011-08-31 11:02 -0700
Message-ID<f55d5c89-7988-42b3-9062-724aaaabd9cf@u20g2000yqj.googlegroups.com>
In reply to#12510
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?

[toc] | [prev] | [next] | [standalone]


#12513

FromIan Kelly <ian.g.kelly@gmail.com>
Date2011-08-31 12:18 -0600
Message-ID<mailman.626.1314814742.27778.python-list@python.org>
In reply to#12511
On Wed, Aug 31, 2011 at 12:02 PM, Travis Parks <jehugaleahsa@gmail.com> wrote:
> Am I doing something wrong, here? nonlocal isn't registering. Which
> version did this get incorporated?

3.0

[toc] | [prev] | [next] | [standalone]


#12514

FromTravis Parks <jehugaleahsa@gmail.com>
Date2011-08-31 11:26 -0700
Message-ID<84d38b41-5137-401d-b3c3-b6cf44451c73@gz5g2000vbb.googlegroups.com>
In reply to#12513
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.

[toc] | [prev] | [next] | [standalone]


#12516

From"bruno.desthuilliers@gmail.com" <bruno.desthuilliers@gmail.com>
Date2011-08-31 11:03 -0700
Message-ID<0d66c6c7-fbbb-4030-8d00-9f260f4c0e22@h11g2000vbc.googlegroups.com>
In reply to#12499
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.

[toc] | [prev] | [next] | [standalone]


#12521

FromTravis Parks <jehugaleahsa@gmail.com>
Date2011-08-31 12:33 -0700
Message-ID<def07904-369c-4ecb-b516-cfa16c88afbb@a7g2000yqb.googlegroups.com>
In reply to#12516
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.

[toc] | [prev] | [next] | [standalone]


#12530

FromTerry Reedy <tjreedy@udel.edu>
Date2011-08-31 18:21 -0400
Message-ID<mailman.634.1314829322.27778.python-list@python.org>
In reply to#12499
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

[toc] | [prev] | [next] | [standalone]


#12769

FromPiet van Oostrum <piet@vanoostrum.org>
Date2011-09-05 16:23 +0200
Message-ID<m2sjobf4pv.fsf@cochabamba.vanoostrum.org>
In reply to#12499
Travis Parks <jehugaleahsa@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 <piet@vanoostrum.org>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web