strings in the global section

Phoe6 orsenthil at
Thu Oct 12 19:44:00 CEST 2006

I write a script:
astring = "This is a String"

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


And I am expecting the output to be:
This is a String
I modify it in fun1
I modify it in fun2

But it is not so. It always prints This is a String. astring declared
outside all the functions, is not in global section and values be
overwritten by functions accessing it?

- How is this working?
- What should I do to overwrite the string variable in the global
section within functions?


More information about the Python-list mailing list