Tim Chase wrote:
> Sounded like the OP wanted a behavior similar to "auto-globals"
> that has been a long-cursed aspect of PHP. That, without
> specifying it, local-variables should be global if their name
> exists in the global namespace.
Sounds like a source of many hard to track down bugs.
>> Works, but something different ?
>
> Excuse me, what do you mean?
Sounded like the OP wanted a behavior similar to "auto-globals"
that has been a long-cursed aspect of PHP. That, without
specifying it, local-variables should be global if their name
exists in the global namespace. Python s
Phoe6 wrote:
> #!/usr/bin/python
> global astring
> astring = "This is a String"
A global declaration just says "look for this name in the module
namespace". Since you /are/ in the module namespace here, there's
no need to put "global" here.
> Works, but something different ?
Excuse me, what d
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
Phoe6 wrote:
> I write a script:
> #!/usr/bin/python
> 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
> fun1()
> print astring
> fun2()
>
Phoe6 wrote:
> 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?
If you assign "astring" inside the function body, it's a local name.
> -
I write a script:
#!/usr/bin/python
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
fun1()
print astring
fun2()
print astring
if __name__ == '__mai