Twisted (or for loops ?) madness

looping kadeko at gmail.com
Mon Oct 15 08:04:15 EDT 2007


On Oct 15, 1:51 pm, Michele Simionato <michele.simion... at gmail.com>
wrote:
> On Oct 15, 1:01 pm, looping <kad... at gmail.com> wrote:
>
> > So if I understand what Michele wrote (thanks too), when a function is
> > defined (with def), no scope is saved and every variable value not
> > passed in parameter is lost ?  It means that variable value come from
> > the outer scope when the function is called ?
>
> Yes, in my example you get the value of "i" at the function *calling*
> time,
> not at the function definition time. If you want to store the value at
> the
> definition time, you must use the default argument trick:
>
> funclist = []
> for i in 1,2,3:
>   def f(i=i):
>      print i
>   funclist.append(f)
>
>        Michele Simionato

Thanks Michele, now I understand how it works and I learned something
new. Not a bad day...




More information about the Python-list mailing list