Paul L. Snyder on 4 Oct 2004 14:58:02 -0000


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

Re: [PLUG] python and global name space


Quoting Jeff Abrahamson <jeff@purple.com>:

> In the snippet below, I'm hoping foo() will side-effect the global
> num.  It doesn't.
[...]
>     >>> global num
>     >>> num = 0
>     >>> def foo():
>     ...     num += 5
>     ...     print num
>     ...
>     >>> num
>     0
>     >>> foo()

I haven't mucked with Python that much, but redeclaring num as
global inside the function seems to do what you are looking for:

>>> global num
>>> num = 0
>>> def foo():
...     global num
...     num += 5
...     print num
...
>>> num
0
>>> foo()
5
>>> num
5

pls

___________________________________________________________________________
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