New submission from WIl Hall w...@wilhall.com:
When creating lambda functions in a loop, variables involved in the lambda
statements appear to not resolve until the loop finishes. This results in all
of the defined lambda functions using the same variable to resolve to the last
value
Matthew Barnett pyt...@mrabarnett.plus.com added the comment:
That's not a bug.
This might help to explain what's going on:
What do (lambda) function closures capture in Python?
http://stackoverflow.com/questions/2295290/what-do-lambda-function-closures-capture-in-python
--
nosy:
Changes by Benjamin Peterson benja...@python.org:
--
resolution: - invalid
status: open - closed
___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue13652
___
WIl Hall w...@wilhall.com added the comment:
Makes sense. Misunderstanding on my part, then. Closing as invalid (not a bug).
--
___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue13652
___
Pierre pierre.gaill...@gmail.com wrote:
I would like to know if it is possible to define a loop in a lambda
function
It is if you can easily replace the for loop with a call to map():
s_minus_1 = lambda s: map(lambda x: x-1, s)
test = range(1, 100, 10)
test
[1, 11, 21, 31, 41, 51, 61,
Hello,
I would like to know if it is possible to define a loop in a lambda
function
How to manage the indents ? Example :
s_minus_1 = lambda s : for index in range(0, len(s)) : s[index] = s
[index]-1
Thanks !
--
http://mail.python.org/mailman/listinfo/python-list
Hello,
This page has some advice about how to avoid some of the lambda
functions limitations:
http://p-nand-q.com/python/stupid_lambda_tricks.html
In particular, it suggests to use map function instead of for loops.
Best regards,
Javier
2009/8/31 Pierre pierre.gaill...@gmail.com:
Hello
On Mon, Aug 31, 2009 at 12:41 AM, Pierrepierre.gaill...@gmail.com wrote:
Hello,
I would like to know if it is possible to define a loop in a lambda
function
Not possible. Lambdas can only contain a single expression. A loop is
a block statement.
Just use a named function instead. There's
En Mon, 31 Aug 2009 04:41:57 -0300, Pierre pierre.gaill...@gmail.com
escribió:
I would like to know if it is possible to define a loop in a lambda
function
How to manage the indents ? Example :
s_minus_1 = lambda s : for index in range(0, len(s)) : s[index] = s
[index]-1
You can't.
Pierre pierre.gaill...@gmail.com writes:
s_minus_1 = lambda s : for index in range(0, len(s)) : s[index] = s
[index]-1
What are you trying to do here anyway? That looks broken.
Maybe you want the list.insert method.
--
http://mail.python.org/mailman/listinfo/python-list
On Mon, 31 Aug 2009 08:41:57 +0100, Pierre pierre.gaill...@gmail.com
wrote:
Hello,
I would like to know if it is possible to define a loop in a lambda
function
How to manage the indents ? Example :
s_minus_1 = lambda s : for index in range(0, len(s)) : s[index] = s
[index]-1
You can't
New submission from Michal Vyskocil mvysko...@suse.cz:
The compounded expressions with lambda functions are evaluated
incorrectly. The simple expressions, or a named functions are evaluated
good. The problem is only in the evaluation of compounded expressions.
It seems that after evaluate
Eric Smith e...@trueblade.com added the comment:
This isn't the right forum to ask for help. You should try
comp.lang.python, where someone would be happy to explain this to you.
Having said that, here's the explanation:
This is not a bug.
Disregarding side effects, the expression:
a = b or c
On Jan 28, 11:32 pm, Gabriel Genellina gagsl-...@yahoo.com.ar
wrote:
En Wed, 28 Jan 2009 16:05:39 -0200, coutinhoti...@gmail.com
coutinhoti...@gmail.com escribió:
I had the same problem myself.
Mark's detailed explanation really helped me understand.
I ended up doing something
Hi!
I had the same problem myself.
Mark's detailed explanation really helped me understand.
I ended up doing something like:
class A:
def __init__(self):
names = 'n1', 'n2'
for n in names:
setattr(self, get%s % n, self._createGetter(n))
def
En Wed, 28 Jan 2009 16:05:39 -0200, coutinhoti...@gmail.com
coutinhoti...@gmail.com escribió:
I had the same problem myself.
Mark's detailed explanation really helped me understand.
I ended up doing something like:
The code doesn't work as-is, could you please post a working version?
Michael Torrie torr...@gmail.com writes:
Basically, don't use a lambda. Create a real, local closure with a
nested def block. That way the closure is created every time the
parent function is called.
Nope. I explained the real problem quite clearly, and it's to do with
the difference
Mark Wooding wrote:
unine...@gmail.com writes:
[...]
* Assignment stores a new (reference to a) value in the variable.
* Binding modifies the mapping between names and variables.
I realise I have omitted what was doubtless intended to be explanatory
detail, but I am having trouble
On 26 Jan., 15:13, Steve Holden st...@holdenweb.com wrote:
Mark Wooding wrote:
unine...@gmail.com writes:
[...]
* Assignment stores a new (reference to a) value in the variable.
* Binding modifies the mapping between names and variables.
I realise I have omitted what was doubtless
Steve Holden st...@holdenweb.com writes:
Mark Wooding wrote:
* Assignment stores a new (reference to a) value in the variable.
* Binding modifies the mapping between names and variables.
I realise I have omitted what was doubtless intended to be explanatory
detail, but I am having
Mark Wooding wrote:
Steve Holden st...@holdenweb.com writes:
Mark Wooding wrote:
* Assignment stores a new (reference to a) value in the variable.
* Binding modifies the mapping between names and variables.
I realise I have omitted what was doubtless intended to be explanatory
On 23 Jan., 13:28, unine...@gmail.com wrote:
Hi,
I want to add some properties dynamically to a class, and then add the
corresponding getter methods. Something resulting in this:
class Person:
def Getname(self):
return self.__name
def Getage(self):
return
unine...@gmail.com wrote:
The attributes are right, but the getter are not working. The problem
is that the lambda function always execute the last parameter passed
for all instances of the methods. How could it be done the right way?
Basically, don't use a lambda. Create a real, local
Hi,
I want to add some properties dynamically to a class, and then add the
corresponding getter methods. Something resulting in this:
class Person:
def Getname(self):
return self.__name
def Getage(self):
return self.__age
I've implemented the next code, creating the
On Fri, 23 Jan 2009 04:28:33 -0800, unineuro wrote:
Hi,
I want to add some properties dynamically to a class, and then add the
corresponding getter methods. Something resulting in this:
class Person:
def Getname(self):
return self.__name
def Getage(self):
unine...@gmail.com wrote:
class Person:
def __init__(self):
for prop in props:
setattr(self, __ + prop[0], prop[1])
setattr(Person, Get + prop[0], lambda self: getattr
(self, __ + prop[0]))
I've had a similar problem here and here is best how I can explain
unine...@gmail.com writes:
class Person:
def __init__(self):
for prop in props:
setattr(self, __ + prop[0], prop[1])
setattr(Person, Get + prop[0], lambda self: getattr
(self, __ + prop[0]))
[...]
The attributes are right, but the getter are not
Hi,
I try to get a set of lambda functions that allows me executing each
function code exactly once. Therefore, I would like to modify the set
function to compare the func_code properties (or the lambda
functions to use this property for comparison).
(The reason is that the real function list
On Sun, 25 May 2008 13:43:15 +0200, Martin Manns wrote:
Hi,
I try to get a set of lambda functions that allows me executing each
function code exactly once. Therefore, I would like to modify the set
function to compare the func_code properties (or the lambda functions to
use this property
On Sun, 25 May 2008 13:43:15 +0200, Martin Manns wrote:
Hi,
I try to get a set of lambda functions that allows me executing each
function code exactly once. Therefore, I would like to modify the set
function to compare the func_code properties (or the lambda functions to
use this property
On Sun, 25 May 2008 12:14:25 + (UTC)
Ivan Illarionov [EMAIL PROTECTED] wrote:
On Sun, 25 May 2008 13:43:15 +0200, Martin Manns wrote:
Maybe make a set of code objects?
func_code_set = set([f.func_code for f in funclist])
funclist = []
for fc in func_code_set:
f = lambda x: x
On Sun, 25 May 2008 13:43:15 +0200, Martin Manns wrote:
I try to get a set of lambda functions that allows me executing each
function code exactly once. Therefore, I would like to modify the set
function to compare the func_code properties (or the lambda functions to
use this property
I V:
You might instead want to
wrap the lambdas in an object that will do the comparison you want:
This looks very nice, I haven't tried it yet, but if it works well
then it may deserve to be stored in the cookbook, or better, it may
become the built-in behavior of hashing functions.
Bye,
This may have some bugs left, but it looks a bit better:
from inspect import getargspec
class HashableFunction(object):
Class that can be used to wrap functions, to allow their
hashing,
for example to create a set of unique functions.
func_strings = ['x', 'x+1', 'x+2', 'x']
On Sun, May 25, 2008 at 1:43 PM, Martin Manns [EMAIL PROTECTED] wrote:
Hi,
I try to get a set of lambda functions that allows me executing each
function code exactly once. Therefore, I would like to modify the set
function to compare the func_code properties (or the lambda
functions to use
On Sun, May 25, 2008 at 1:43 PM, Martin Manns [EMAIL PROTECTED] wrote:
Hi,
I try to get a set of lambda functions that allows me executing each
function code exactly once. Therefore, I would like to modify the set
function to compare the func_code properties (or the lambda
functions to use
Denis Kasak wrote:
...
spam = []
for i in range(10):
... spam.append(lambda: i)
spam[0]()
9
spam[1]()
9
Manually creating the lambdas and appending them to a list works as
expected, naturally; I don't see a good reason why it wouldn't work
with a loop. Am I missing something?
Yes, you
: global name 'i' is not defined.
Ah, the problem was in the subtle misunderstanding of the semantics of
lambda functions on my part. It's much clearer now. Thanks.
There are a couple of ways to slve your problem:
(1) use default args to do the binding at the function definition time.
for i
On Sun, 25 May 2008 14:39:28 -0700 (PDT)
[EMAIL PROTECTED] wrote:
This may have some bugs left, but it looks a bit better:
[...]
self._hash = hash(self._func.func_code) ^ \
hash(tuple(signature[0]) + tuple(signature[1:3]))
def __eq__(self, other):
I V [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
| On Sun, 25 May 2008 13:43:15 +0200, Martin Manns wrote:
| I try to get a set of lambda functions that allows me executing each
I think it worth the reminder that Python has lambda *expressions* that
result in function objects
Martin Manns [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
| On Sun, 25 May 2008 14:39:28 -0700 (PDT)
| [EMAIL PROTECTED] wrote:
|
| This may have some bugs left, but it looks a bit better:
| [...]
| self._hash = hash(self._func.func_code) ^ \
|
Martin Manns wrote:
Hi,
I try to get a set of lambda functions that allows me executing each
function code exactly once. Therefore, I would like to modify the set
function to compare the func_code properties (or the lambda
functions to use this property for comparison).
(The reason
This means that f is not a pointer to make_incrementor but rather to
the internal (copied?) function.
returned function isthe right here. As any returned object from a function.
This style is very common in Scheme programming so you might read a
Scheme book if you want to understand it.
Hello,
I'm new on this list and in python.
It seems python has some interesting concept of ad hoc function
which I'm trying to understand without much success.
Take the following code for example:
def make_incrementor(n):
... return lambda x: x + n
...
f = make_incrementor(42)
f(0)
42
f(1)
Maybe you would like a generator:
def f(n):
... while True:
... n += 1
... yield n
...
a = f(5)
a.next()
6
a.next()
7
a.next()
8
a.next()
9
On 2/5/07, Maxim Veksler [EMAIL PROTECTED] wrote:
Hello,
I'm new on this list and in python.
It seems python has
Maxim Veksler [EMAIL PROTECTED] writes:
def make_incrementor(n):
... return lambda x: x + n
Is the same as:
def make_incrementor(n):
def inner(x):
return x + n
return inner
When you enter make_incrementor, it allocates a memory slot (normally
we'd think of this as a stack
Maxim Veksler a écrit :
Hello,
I'm new on this list and in python.
Welcome on board...
It seems python has some interesting concept of ad hoc function
which I'm trying to understand without much success.
Take the following code for example:
def make_incrementor(n):
... return
Maxim Veksler wrote:
And what is the f object? An integer? a pointer? an Object?
A function.
--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/CSS/Javascript/SQL/Perl/PHP/Python*/Apache/Linux
* = I'm getting there!
--
Wow,
Thank you everyone for the help. I am amazed by the motivation people
have on this list to help new comers. I hope that I will be able to
contribute equally some day.
On 05 Feb 2007 14:22:05 -0800, Paul Rubin
http://phr.cx@nospam.invalid wrote:
Maxim Veksler [EMAIL PROTECTED] writes:
Le Mercredi 24 Mai 2006 22:37, Scott David Daniels a écrit :
class Base(object):
def __init__(self, attr):
self._attr = attr
def getattr(self):
return self._attr
def attr(self):
return self.getattr()
attr =
Oh! Thanx! Great! this is what i was looking for! :)
Scott David Daniels ha escrito:
Pablo wrote:
Second solution: This is what i want, but...
class Base(object):
def __init__(self, attr):
self._attr = attr
def getattr(self):
return self._attr
attr =
Pablo wrote:
Second solution: This is what i want, but...
class Base(object):
def __init__(self, attr):
self._attr = attr
def getattr(self):
return self._attr
attr = property(fget=lambda self: self.getattr())
class Derived(Base):
def getattr(self):
Hello all, sorry if this is a faq...
Problem: The intended effect is to override the method 'getattr' in a
way that i dont need to override the property explicitly too.
class Base(object):
def __init__(self, attr):
self._attr = attr
def getattr(self):
return self._attr
Pablo ha escrito:
Hello all, sorry if this is a faq...
Problem: The intended effect is to override the method 'getattr' in a
way that i dont need to override the property explicitly too.
class Base(object):
def __init__(self, attr):
self._attr = attr
def getattr(self):
Le Mardi 23 Mai 2006 15:55, Pablo a écrit :
Question: Isn't there an *alternative* way to do it without the lambda
function?
No, it's good, why shouldn't you use a lambda function ?
Note you can do something like this :
class _virtualgetter :
def __init__(self, name) : self._n =name
The reason i would like a different approach to the lambda function is
just a question of personal taste... i dont really like it.
thanx!
--
http://mail.python.org/mailman/listinfo/python-list
thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Interpreter Core
Group: Python 2.4
Status: Closed
Resolution: Invalid
Priority: 5
Submitted By: Samuel Hsiung (shsiung)
Assigned to: Nobody/Anonymous (nobody)
Summary: lambda functions
OK.
The thing i've got is an obscure semantic bug, occured because of my
unawareness of the following Python features:
1. (In major)
http://mail.python.org/pipermail/python-dev/2005-September/056508.html
2. late bindings of the function's body
Got to know! :)
Thanks for your attention.
--
Valid link in my previews message is
http://mail.python.org/pipermail/python-dev/2005-September/056669.html
Sorry.
--
http://mail.python.org/mailman/listinfo/python-list
Hello!
Please take a look at the example.
a = [(x, y) for x, y in map(None, range(10), range(10))] # Just a list of
tuples
a
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8,
8), (9, 9)]
Now i want to get a list of functions x*y/n, for each (x, y) in a:
funcs = [lambda
Max Rybinsky [EMAIL PROTECTED] wrote:
...
funcs = [lambda n: x * y / n for x, y in a]
...
It seems, all functions have x and y set to 9.
What's wrong with it? Is it a bug?
It's known as *late binding*: names x and y are looked up when the
lambda's body is executing, and at that time
On 29 Oct 2005 14:25:24 -0700, Max Rybinsky [EMAIL PROTECTED] wrote:
Hello!
Please take a look at the example.
a = [(x, y) for x, y in map(None, range(10), range(10))] # Just a list of
tuples
a
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8,
8), (9, 9)]
Now i want to
Thank you for explanation, Alex.
It appears that almost every beginner to Python gets in trouble with
this ...feature. :)
--
http://mail.python.org/mailman/listinfo/python-list
Max Rybinsky [EMAIL PROTECTED] wrote:
Thank you for explanation, Alex.
It appears that almost every beginner to Python gets in trouble with
this ...feature. :)
Almost every beginner to Python gets in trouble by expecting do what
I'm thinking of RIGHT NOW-binding, which no language offers: in
64 matches
Mail list logo