Re: [PLUG] python and global name space

Dan Widyono on 4 Oct 2004 15:27:03 -0000


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] python and global name space


> You have to declare num as global in the *function*, not in the outer
> scope.
>From Python Essential Reference, 2nd ed., p66:
"One peculiarity of namespaces is the manipulation of global variables from
within a function. ... When variables are assigned in a function, they're
always bound to the function's local namespace; ... To alter this behavior,
use the global statement. ... is necessary only when global variables will be
modified."
This includes e.g.
a = 42
def foo():
 print a
 a = 13
 print a
foo()
This will error, since "a = 13" says you expect a to be local within the
function, so even the first "print a" is referencing a local variable, which
is yet undefined.
Dan W.
-- 
-- Daniel Widyono --
-- www.widyono.net --
-- www.cis.upenn.edu/~widyono --
-- 
___________________________________________________________________________
Philadelphia Linux Users Group -- http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion -- http://lists.phillylinux.org/mailman/listinfo/plug



AltStyle によって変換されたページ (->オリジナル) /