On 2007-08-14, stef mientki <[EMAIL PROTECTED]> wrote: > I've thought many times I finally understood the import / > namespace rules, but again I'm totally lost :-( > > This is my library file > > # Module lib_test.py > > X = 1 > > def Init(): > global X > X = 3 > print 'Init', X > > def Run (): > print X <=== UnboundLocalError: local variable > 'X' referenced before assignment > X = X + 1 > print ' Run', X
>From _Python Language Reference Manual_ see: '4.1 Naming and Binding' and '6.13 The global statement'. If a name is bound [assigned] in a block, it is a local variable of that block. ... The global statement is a declaration which holds for the entire current code block. It means that the listed identifiers are to be interpreted as globals. It would be impossible to assign to a global variable without global, although free variables may refer to globals without being declared global. Since your Init and Run functions both assign (bind) a value to X, X is assumed to be a local variable unless you say otherwise with a global statement. Note that the global statement in Init applies only in Init's code block. Run must have it's own 'global X' statement, if applicable. -- Neil Cerutti -- http://mail.python.org/mailman/listinfo/python-list