strings in the global section

Phoe6 orsenthil at gmail.com
Thu Oct 12 13:52:13 EDT 2006


Bjoern Schliessmann wrote:
> If you assign "astring" inside the function body, it's a local name.
>
> > - What should I do to overwrite the string variable in the global
> > section within functions?
>
> Put a "global astring" in the function to access the global name
> instead.

#!/usr/bin/python
global astring
astring = "This is a String"

def fun1():
        global astring
        astring = "I modify it in fun1"
def fun2():
        global astring
        astring = "I modify it in fun2"
def main():
        print astring
        fun1()
        print astring
        fun2()
        print astring
if __name__ == '__main__':
        main()
~


~                         
Works, but something different ?




More information about the Python-list mailing list