lambda rewrite question

classic Classic list List threaded Threaded
16 messages Options
Reply | Threaded
Open this post in threaded view
|

lambda rewrite question

Floris van Manen
For those of you who know everything about the basic python syntax & semantics ...
I get stuck in a simple context issue (i think)


this version works, it return the stored float value

def rewrite( d ):
    for key in d:
        for k in d[key]['next']:
            c = float(d[key]['next'][k]['prob'])
            d[key]['next'][k]['prob'] = c
    return d



this doesn't
if i call the function it will return a (random?) identical value for all stored lambda functions.

def rewrite( d ):
    for key in d:
        for k in d[key]['next']:
            c = float(d[key]['next'][k]['prob'])
            d[key]['next'][k]['prob'] = lambda : c
    return d


There is most likely a reason to it.
Can someone explain me what that reason is ?

Thanks!
.Floris


_______________________________________________
Python-nl mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-nl
Reply | Threaded
Open this post in threaded view
|

Re: lambda rewrite question

Tikitu de Jager -- Buzzcapture
There's a good StackOverflow answer on this:

http://stackoverflow.com/questions/2295290/what-do-lambda-function-closures-capture-in-python

The question is actually by my boss, but that's something of a
coincidence: I found it with google then remembered that he had stumbled
on the problem before.

gr,
Tikitu

--
Tikitu de Jager
_______________________________________________
Python-nl mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-nl
Reply | Threaded
Open this post in threaded view
|

Re: lambda rewrite question

Sylvain Viollon
In reply to this post by Floris van Manen
On Wed, 29 Jun 2011 16:01:38 +0200
Floris van Manen <[hidden email]> wrote:

  Hello,

> this doesn't
> if i call the function it will return a (random?) identical value for
> all stored lambda functions.
>
> def rewrite( d ):
>     for key in d:
>         for k in d[key]['next']:
>             c = float(d[key]['next'][k]['prob'])
>             d[key]['next'][k]['prob'] = lambda : c
>     return d
>
>
> There is most likely a reason to it.
> Can someone explain me what that reason is ?
>

  c refer to only one variable, which scope is the function rewrite.

  lambda here create a closure that return the value of this unique
  variable, that will have as value the one computed in the last
  iteration of your loop (that's not random).

  Regards,

  Sylvain,

--
Sylvain Viollon -- Infrae
t +31 10 243 7051 -- http://infrae.com
Hoevestraat 10 3033GC Rotterdam -- The Netherlands
_______________________________________________
Python-nl mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-nl
Reply | Threaded
Open this post in threaded view
|

Re: lambda rewrite question

Elit
In reply to this post by Floris van Manen
Hi,

I think the problem is that the lamba function tries to return the value stored
at variable 'c', but that variable is only defined within the inner loop. So, if
the lambda function gets called later on, it references 'c' which is not valid
anymore.

You might be able to solve it by  doing:

d[key]['next'][k]['prob'] = lambda : float(c)

(So, move the float function into the lambda)

I hope this works (and that I'm not wrong :) )

Good luck,

Rinze



On 29-6-2011 16:01, Floris van Manen wrote:

> For those of you who know everything about the basic python syntax&  semantics ...
> I get stuck in a simple context issue (i think)
>
>
> this version works, it return the stored float value
>
> def rewrite( d ):
>      for key in d:
>          for k in d[key]['next']:
>              c = float(d[key]['next'][k]['prob'])
>              d[key]['next'][k]['prob'] = c
>      return d
>
>
>
> this doesn't
> if i call the function it will return a (random?) identical value for all stored lambda functions.
>
> def rewrite( d ):
>      for key in d:
>          for k in d[key]['next']:
>              c = float(d[key]['next'][k]['prob'])
>              d[key]['next'][k]['prob'] = lambda : c
>      return d
>
>
> There is most likely a reason to it.
> Can someone explain me what that reason is ?
>
> Thanks!
> .Floris
>
>
> _______________________________________________
> Python-nl mailing list
> [hidden email]
> http://mail.python.org/mailman/listinfo/python-nl
>
_______________________________________________
Python-nl mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-nl
Reply | Threaded
Open this post in threaded view
|

Re: lambda rewrite question

Wichert Akkerman
In reply to this post by Sylvain Viollon
On 06/29/2011 04:30 PM, Sylvain Viollon wrote:

> On Wed, 29 Jun 2011 16:01:38 +0200
> Floris van Manen<[hidden email]>  wrote:
>
>    Hello,
>
>> this doesn't
>> if i call the function it will return a (random?) identical value for
>> all stored lambda functions.
>>
>> def rewrite( d ):
>>      for key in d:
>>          for k in d[key]['next']:
>>              c = float(d[key]['next'][k]['prob'])
>>              d[key]['next'][k]['prob'] = lambda : c
>>      return d
>>
>>
>> There is most likely a reason to it.
>> Can someone explain me what that reason is ?
>>
>    c refer to only one variable, which scope is the function rewrite.
>
>    lambda here create a closure that return the value of this unique
>    variable, that will have as value the one computed in the last
>    iteration of your loop (that's not random).

(zullen we python-nl gewoon in het nederlands houden?)

Als workaround kan je dit herschrijven als "lambda x=c: x"

Wichert.

_______________________________________________
Python-nl mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-nl
Reply | Threaded
Open this post in threaded view
|

Opschonen programma (was: lambda rewrite question)

A.T.Hofkamp
In reply to this post by Floris van Manen
On 29/06/11 16:01, Floris van Manen wrote:

> For those of you who know everything about the basic python syntax&  semantics ...
> I get stuck in a simple context issue (i think)
>
>
> this version works, it return the stored float value
>
> def rewrite( d ):
>      for key in d:
>          for k in d[key]['next']:
>              c = float(d[key]['next'][k]['prob'])
>              d[key]['next'][k]['prob'] = c
>      return d

Anderen hebben je vraag al opgelost, ik wil alleen even opmerken dat je programma mooier kan met
behulp van d.itervalues():

def rewrite( d ):
      for v in d.itervalues():
          for w in v['next'].itervalues():
              c = float(w['prob'])
              w['prob'] = c
      return d


Verder is de 'return' is niet nodig, de aanroeper heeft al een referentie naar d.

In mijn programma's betekent teruggeven van een waarde na een aanroep altijd 'hier heb je een nieuwe
waarde, de oude is ongewijzigd'. Dat geldt hier niet. Weglaten van de return maakt dat meer expliciet.


Albert
_______________________________________________
Python-nl mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-nl
Reply | Threaded
Open this post in threaded view
|

Re: Opschonen programma (was: lambda rewrite question)

Floris van Manen
A.

Dat ruimt inderdaad mooi op.
De leesbaarheid is er vooralsnog toch nog vooral voor de mens achter de programmeur :-)
Dank!

.F


On Jun 29, 2011, at 16:53, A.T.Hofkamp wrote:

> On 29/06/11 16:01, Floris van Manen wrote:
>> For those of you who know everything about the basic python syntax&  semantics ...
>> I get stuck in a simple context issue (i think)
>>
>>
>> this version works, it return the stored float value
>>
>> def rewrite( d ):
>>     for key in d:
>>         for k in d[key]['next']:
>>             c = float(d[key]['next'][k]['prob'])
>>             d[key]['next'][k]['prob'] = c
>>     return d
>
> Anderen hebben je vraag al opgelost, ik wil alleen even opmerken dat je programma mooier kan met behulp van d.itervalues():
>
> def rewrite( d ):
>     for v in d.itervalues():
>         for w in v['next'].itervalues():
>             c = float(w['prob'])
>             w['prob'] = c
>     return d
>
>
> Verder is de 'return' is niet nodig, de aanroeper heeft al een referentie naar d.
>
> In mijn programma's betekent teruggeven van een waarde na een aanroep altijd 'hier heb je een nieuwe waarde, de oude is ongewijzigd'. Dat geldt hier niet. Weglaten van de return maakt dat meer expliciet.
>
>
> Albert
> _______________________________________________
> Python-nl mailing list
> [hidden email]
> http://mail.python.org/mailman/listinfo/python-nl

_______________________________________________
Python-nl mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-nl
Reply | Threaded
Open this post in threaded view
|

Re: lambda rewrite question

Floris van Manen
In reply to this post by Wichert Akkerman

On Jun 29, 2011, at 16:44, Wichert Akkerman wrote:

> Als workaround kan je dit herschrijven als "lambda x=c: x"

Dat is ook weer wel zo duidelijk.

.F

_______________________________________________
Python-nl mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-nl
Reply | Threaded
Open this post in threaded view
|

Re: lambda rewrite question

kit BLAKE-2
In reply to this post by Wichert Akkerman
On 29 Jun 2011, at 16:44, Wichert Akkerman wrote:
> (zullen we python-nl gewoon in het nederlands houden?)

Hij is een Fransman. (Maar toch bezig met een cursus nederlands :)

Kit


> Als workaround kan je dit herschrijven als "lambda x=c: x"
>
> Wichert.
>
> _______________________________________________
> Python-nl mailing list
> [hidden email]
> http://mail.python.org/mailman/listinfo/python-nl

--
Kit BLAKE · Infrae · http://infrae.com/ + 31 10 243 7051

_______________________________________________
Python-nl mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-nl
Reply | Threaded
Open this post in threaded view
|

Re: lambda rewrite question

Wichert Akkerman
On 06/29/2011 05:35 PM, Kit BLAKE wrote:
> On 29 Jun 2011, at 16:44, Wichert Akkerman wrote:
>> (zullen we python-nl gewoon in het nederlands houden?)
>
> Hij is een Fransman. (Maar toch bezig met een cursus nederlands :)

Prima reden om hier nederlands te proberen dan! En anders is er vast
python-fr :)


_______________________________________________
Python-nl mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-nl
Reply | Threaded
Open this post in threaded view
|

Re: lambda rewrite question

Sylvain Viollon
On Wed, 29 Jun 2011 17:36:11 +0200
Wichert Akkerman <[hidden email]> wrote:

> On 06/29/2011 05:35 PM, Kit BLAKE wrote:
> > On 29 Jun 2011, at 16:44, Wichert Akkerman wrote:
> >> (zullen we python-nl gewoon in het nederlands houden?)
> >
> > Hij is een Fransman. (Maar toch bezig met een cursus nederlands :)
>
> Prima reden om hier nederlands te proberen dan! En anders is er vast
> python-fr :)
>

  En de vraag was in engels.

  Sylvain,

--
Sylvain Viollon -- Infrae
t +31 10 243 7051 -- http://infrae.com
Hoevestraat 10 3033GC Rotterdam -- The Netherlands
_______________________________________________
Python-nl mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-nl
Reply | Threaded
Open this post in threaded view
|

Re: lambda rewrite question

Floris van Manen

On Jun 29, 2011, at 17:51, Sylvain Viollon wrote:

> On Wed, 29 Jun 2011 17:36:11 +0200
> Wichert Akkerman <[hidden email]> wrote:
>
>> On 06/29/2011 05:35 PM, Kit BLAKE wrote:
>>> On 29 Jun 2011, at 16:44, Wichert Akkerman wrote:
>>>> (zullen we python-nl gewoon in het nederlands houden?)
>>>
>>> Hij is een Fransman. (Maar toch bezig met een cursus nederlands :)
>>
>> Prima reden om hier nederlands te proberen dan! En anders is er vast
>> python-fr :)
>>
>
>  En de vraag was in engels.


Exactement ...
En python zelf is ook niet helemaal 100% autochtoon, qua syntax en alles :-)

.F


_______________________________________________
Python-nl mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-nl
Reply | Threaded
Open this post in threaded view
|

Re: lambda rewrite question

Rob Hooft
In reply to this post by Floris van Manen
2011/6/29 Floris van Manen <[hidden email]>:

> For those of you who know everything about the basic python syntax & semantics ...
> I get stuck in a simple context issue (i think)
>
>
> this version works, it return the stored float value
>
> def rewrite( d ):
>    for key in d:
>        for k in d[key]['next']:
>            c = float(d[key]['next'][k]['prob'])
>            d[key]['next'][k]['prob'] = c
>    return d
>
>
>
> this doesn't
> if i call the function it will return a (random?) identical value for all stored lambda functions.
>
> def rewrite( d ):
>    for key in d:
>        for k in d[key]['next']:
>            c = float(d[key]['next'][k]['prob'])
>            d[key]['next'][k]['prob'] = lambda : c
>    return d
>
>
> There is most likely a reason to it.
> Can someone explain me what that reason is ?

The latter function will set all values to the same function,
returning the value of "c". So, they will all report the last value of
"c". Your lambda is almost the same as defining the function
elsewhere:

 def rewrite( d ):
    def utility():
          return c
    for key in d:
        for k in d[key]['next']:
            c = float(d[key]['next'][k]['prob'])
            d[key]['next'][k]['prob'] = utility
    return d

Rob

--
Rob W. W. Hooft || [hidden email] || http://hooft.net/rob
_______________________________________________
Python-nl mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-nl
Reply | Threaded
Open this post in threaded view
|

Re: lambda rewrite question

Remco Wendt-2
In reply to this post by Sylvain Viollon
2011/6/29 Sylvain Viollon <[hidden email]>
 En de vraag was in engels.

Gaat al de goede kant op Sylvain! Het is alleen: de vraag was in _het_ Engels.

Groetjes,
Remco
-- 
Maykin Media
Herengracht 416, 1017 BZ Amsterdam
tel.: +31 (0)20 753 05 23
mob.: +31 (0)6 187 967 06
http://www.maykinmedia.nl


_______________________________________________
Python-nl mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-nl
Reply | Threaded
Open this post in threaded view
|

Re: lambda rewrite question

Dirkjan Ochtman
In reply to this post by Floris van Manen
2011/6/29 Floris van Manen <[hidden email]>:
> Exactement ...
> En python zelf is ook niet helemaal 100% autochtoon, qua syntax en alles :-)

Nouja, de syntax zelf is vrij taalneutraal... ;)

Groet,

Dirkjan
_______________________________________________
Python-nl mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-nl
Reply | Threaded
Open this post in threaded view
|

Re: lambda rewrite question

Floris van Manen

On Jun 29, 2011, at 21:03, Dirkjan Ochtman wrote:

> 2011/6/29 Floris van Manen <[hidden email]>:
>> Exactement ...
>> En python zelf is ook niet helemaal 100% autochtoon, qua syntax en alles :-)
>
> Nouja, de syntax zelf is vrij taalneutraal... ;)

( try if def while else not except )
autochtoon in de zin van een stukje polder beleving ...

.F
_______________________________________________
Python-nl mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-nl