Twisted (or for loops ?) madness

Michele Simionato michele.simionato at gmail.com
Mon Oct 15 07:51:41 EDT 2007


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




More information about the Python-list mailing list