lambda closure question

A

Antoon Pardon

Op 2005-02-21 said:
It's not only that way in python, but in java too. So it seems that there is
a fundamental principle behind it: In a language that allows sideeffects,
these will actually happen.

If you're after side-effect free programming, I recommend using a functional
programming language like ocaml or haskell. In these, you can't mutate
objects at all, only recreate them and rebind them to new variables. This
prevents whole classes of errors - but of course it also introduces all
kinds of other constraints on your programming style, e.g. using monads and
so on.

I have the impression that you misunderstood me. I'm not after a
side-effect free language. I just think python could be nicer in
allowing some side-effects.
 
P

Paul Rubin

Diez B. Roggisch said:
It's not only that way in python, but in java too. So it seems that there is
a fundamental principle behind it: In a language that allows sideeffects,
these will actually happen.

Can you even have nested functions in Java? Algol-60 did things the
way you'd expect, i.e. you can modify the outer variable from the
inner scope.
 
D

Diez B. Roggisch

Paul said:
Can you even have nested functions in Java? Algol-60 did things the
way you'd expect, i.e. you can modify the outer variable from the
inner scope.

Not nested functions, but closures in anonymous inner classes - which can
reference only final variables, thus creating the same effect as the
scoping rules of python.
 
D

Diez B. Roggisch

I have the impression that you misunderstood me. I'm not after a
side-effect free language. I just think python could be nicer in
allowing some side-effects.

Yeah, seems as if I somehow added an inadvertent "not" somewhere in my train
of thoughts while reading (and hopfully comprehending...) your post. Sorry.
 
S

Steven Bethard

Antoon said:
... l = []
... def pop():
... return l.pop()
... def push(e):
... l.append(e)
... return pop, push
...

Just a side note to point out that another way of writing this is:

py> def F():
.... l = []
.... return l.pop, l.append
....

You'll get the same behavior:

py> pop, push = F()
py> push(1)
py> pop()
1
py> push(2)
py> push(3)
py> pop()
3
py> pop()
2

Hooray for bound methods! ;)

STeVe
 
J

jfj

Antoon said:
Op 2005-02-19 said:
once foo() returns there is no way to modify 'x'!
It becomes a kind of constant.


In this particular case yes. But not in general, what about
this:


... l = []
... def pop():
... return l.pop()
... def push(e):
... l.append(e)
... return pop, push
...

Others will point this out, but if I'm fast enough...

This does not change the object referenced by l.
It calls methods of it and because it is mutable the containts
of the list are modified.
'l' is a list at address, 0xXXXX and that can never change once
F() has returned.


jfj
 
J

jfj

Carl said:
transformations gets rebound, so you'd need a reference to it.

That certainly is an application. I guess it depends on one's
programming background.

I'd only use nested (function, class) definition to accomplish
such a feature:

########################
def genclass(x,y):
class myclass:
M = x
def f(self, z):
return self.M + y + z
return myclass

A=genclass(1,2)
a=A()
#########################

where i would prefer python to expand this as a template to:

class myclass:
M = 1
def f(self, z):
return self.M + 2 + z
A = myclass
a = A()

IOW, I'd like nested (function, class) definitions to
be used *only* for dynamic code generation with runtime
constants, and OOP *forced* for other applications like
the fractal example:)

jf
 
K

Karl Anderson

Ted Lilley said:
What I want to do is pre-load functions with arguments by iterating
through a list like so:
class myclass: ... pass
def func(self, arg): ... print arg
mylist = ["my", "sample", "list"]
for item in mylist:
... setattr(myclass, item, lamdba self: func(self, item))

I just found myself doing something similar to deal with unittest.py's
test loaders/runners, which have to be able to find attributes on a
class before the class is instantiated.

def addItemStateTransTest(klass):
"""
For each item generator and transition test, add a method to klass
to test that combo.
"""
for ig_name in klass.item_generators:
for tt_name in klass.transition_tests.keys():
test_name = "test" + ig_name + tt_name
test_fun = (lambda self,
ig_name=ig_name, tt_name=tt_name:
self.doTestItemStateTrans(ig_name, tt_name))
setattr(klass, test_name, test_fun)
 

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

Similar Threads


Members online

Forum statistics

Threads
474,221
Messages
2,571,130
Members
47,747
Latest member
swapote

Latest Threads

Top