Closures and Partial Function Application

T

Travis Parks

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)
 
A

Arnaud Delobelle

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
 
T

Travis Parks

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.
 
T

Travis Parks

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?
 
B

bruno.desthuilliers

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.
 
T

Travis Parks


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.
 
T

Travis Parks

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.
 
T

Terry Reedy

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
 
P

Piet van Oostrum

Travis Parks said:
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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,056
Messages
2,570,441
Members
47,119
Latest member
NoeliaIrby

Latest Threads

Top