I thought I'd 'got' globals but...

Luis M. González luismgz at gmail.com
Fri Jul 7 19:41:36 EDT 2006


nate wrote:
> try this:
>
> gname = 'nate'
> def test():
>    gname = 'amy'
>    print gname
>
> test()
> print gname
>
> outputs:
> 'amy'
> 'nate'
>
> whereas this:
> gname = 'nate'
> def test():
>    global gname
>    gname = 'amy'
>    print gname
>
> test()
> print gname
>
> outputs:
> 'amy'
> 'amy'

OK, so I should include the global only if I plan to modify it.
Otherwise, I don't need to include it. Am I right?




More information about the Python-list mailing list