Pierre 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, 71, 81, 91]
>>>
On Mon, 31 Aug 2009 08:41:57 +0100, Pierre
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 use commands in a lambda
Pierre 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
En Mon, 31 Aug 2009 04:41:57 -0300, Pierre
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. lambda is just a way to defin
On Mon, Aug 31, 2009 at 12:41 AM, Pierre 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 nothing that can be do
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 :
> Hello,
>
> I would lik
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
On Jan 28, 11:32 pm, "Gabriel Genellina"
wrote:
> En Wed, 28 Jan 2009 16:05:39 -0200, 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-i
En Wed, 28 Jan 2009 16:05:39 -0200, 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? Just
for the record,
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 _createGetter(se
Mark Wooding wrote:
> Steve Holden 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
Steve Holden 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 trouble rec
On 26 Jan., 15:13, Steve Holden 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 intended t
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 r
Michael Torrie 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 between binding and
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 clos
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 ar
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 ex
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):
>
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 prope
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 is that
"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) ^ \
| >
"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
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):
>
your spam functions, you'll get an
"NameError: 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) u
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 a
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 t
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 t
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']
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,
bear
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
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 = lam
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 function
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 function
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
> 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 underst
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]> write
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!
--
http://mail.python.org/mai
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):
"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
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 pytho
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)
>>>
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()
> att
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._at
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(
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
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
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(s
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
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
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.
--
h
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 offer
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
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)
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 t
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
57 matches
Mail list logo