Hello
I ran the following code in python:
>>> ls = [(lambda op: op + i) for i in [1,2,3]]
>>> ls
[ at 0xb7de4cdc>, at 0xb7de4d14>,
at 0xb7de4d4c>]
>>> for l in ls:
... print l(5)
...
8
8
8
>>>
I am quite surprised of the result. I'm generating three lamdas. What I
want to do is that the first lamda adds 1 to the operand and returns it,
the second lamda return 2 plus the operand and so on.
However all the three lamdas, despite being not the same object in
memory, are all adding 3, so they've got to have a reference to and not
to 1,2,3 respectively.
Why are the lamdas having a reference to the same integer? What am I
getting wrong here? :P
On Sat, 2007-03-03 at 17:24 +0100, [EMAIL PROTECTED] wrote:
> Send Tutor mailing list submissions to
> tutor@python.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
> http://mail.python.org/mailman/listinfo/tutor
> or, via email, send a message with subject or body 'help' to
> [EMAIL PROTECTED]
>
> You can reach the person managing the list at
> [EMAIL PROTECTED]
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Tutor digest..."
>
>
> Today's Topics:
>
>1. Re: geeks like us and the rest of THEM (Andrei)
>2. Re: dictionaries and memory handling (Andrei)
>3. Re: Yet another list comprehension question (Kent Johnson)
>4. trouble understanding the python environment in OSX
> (Clay Wiedemann)
>5. Re: trouble understanding the python environment in OSX
> (Alan Gauld)
>6. Re: trouble understanding the python environment in OSX
> (Kent Johnson)
>7. Re: Yet another list comprehension question (Andrei)
>8. Re: Yet another list comprehension question (Kent Johnson)
>
>
> --
>
> Message: 1
> Date: Sat, 03 Mar 2007 13:40:05 +0100
> From: Andrei <[EMAIL PROTECTED]>
> Subject: Re: [Tutor] geeks like us and the rest of THEM
> To: tutor@python.org
> Message-ID: <[EMAIL PROTECTED]>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
>
> > That's part of it. a good installer is another, and I am loo]king at a
> > couple. A solid and drool-proof server is the remaining leg of the
> > tripod. So far, none of the servers I have seen are satisfactory on all
> > scores- simplicity to install, reliability, economy of cost, (free or
> > unlimited license to me for a modest fee) and ease to configure if the
> > use\r so wishes. the closest I have yet seen SMALL HTTP SERVER. And it
> > is very good.
> > http://smallsrv.com/
>
> You could have a look at Aprelium's Abyss server (X1, which is freely
> distributable in unmodified form IIRC, but closed source). It has a good
> looking webbased configuration console and is cross-platform if you ever
> need that. However, you'll probably end up digging in its config
> programmatically to set up paths and such for the user - shouldn't be
> too hard using some webscraping if the files are not readily modifiable.
> Its setup is about 350 kB.
>
> In terms of installers, NSIS (with the package to make it look modern,
> instead of that awful default look - forgot its name) or InnoSetup are
> the usual suspects. NSIS has a smaller overhead, but an uglier language.
>
> --
> Yours,
>
> Andrei
>
> =
> Mail address in header catches spam. Real contact info:
> ''.join([''.join(s) for s in zip(
> "[EMAIL PROTECTED] pmfe!Pes ontuei ulcpss edtels,s hr' one oC.",
> "rjc5wndon.Sa-re laed o s npbi ot.Ira h it oteesn edt C")])
>
>
>
> --
>
> Message: 2
> Date: Sat, 03 Mar 2007 14:18:24 +0100
> From: Andrei <[EMAIL PROTECTED]>
> Subject: Re: [Tutor] dictionaries and memory handling
> To: tutor@python.org
> Message-ID: <[EMAIL PROTECTED]>
> Content-Type: text/plain; charset=windows-1252; format=flowed
>
> > But most of my dictionaries are nested, and since both keys and values
> > in the dbm 'dictionaries' have to be strings, I can't immediately see
> > how I could get it to work.
> >
> > A bit more detail: I deal with conditional probabilities, with up to 4
> > parameters. These parameters are numbers or words and determine the
> > value (which is always a number). E.g. I have a dictionary
> > {p1:{p2:{p3:{p4:value, where the p's are different parameters. I
> > sometimes need to sum over one or more of the parameters ? for now I
> > have managed to structure the dictionaries so that I only need to sum
> > over the innermost parameter, although this has been a bit cumbersome.
>
> Depends a bit on how many keys each of the dictionaries is going to have
> and in what order they're filled. You can pickle/cPickle an arbitrary
> amount of data as one value, so the whole {p2:{p3:{p4:value}}} story
> could be a value of the p1 key in the bsddb. However, you may not like
> this if you need to retrieve p1 and add new stuff to it all the time,
> because the pickling and