473,385 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

strings in the global section

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__ == '__main__':
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?

Thanks,
Senthil

Oct 12 '06 #1
6 1274
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.
- 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.

BTW, think about using a better interface than globals.

Regards,
Björn

--
BOFH excuse #23:

improperly oriented keyboard

Oct 12 '06 #2
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 ?

Oct 12 '06 #3
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()
print astring
if __name__ == '__main__':
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?
http://docs.python.org/ref/global.html

def fun2():
global astring
astring = 'hey, I've changed!'

-tkc

Oct 12 '06 #4
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 do you mean?

Regards,
Björn

--
BOFH excuse #410:

Electrical conduits in machine room are melting.

Oct 12 '06 #5
>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 seems to behave this way
when reading, but when writing, writes to the local namespace.
Moderately confusing when first encountered, but it makes sense
in short order.

If auto-writing to globals were permitted, all sorts of weird
behaviors could ensue and accompanying breakage of existing code.
I'm quite happy with not using "global" at all (okay, there's
been once or twice I've wanted to modify a module-level settings
structure, but otherwise...).

For the most part, using "global" is cause to be smacked in the
head as it makes for a nightmare maint.-wise.

-tkc

Oct 12 '06 #6
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.
I'm quite happy with not using "global" at all (okay, there's
been once or twice I've wanted to modify a module-level settings
structure, but otherwise...).
AOL
For the most part, using "global" is cause to be smacked in the
head as it makes for a nightmare maint.-wise.
ACK.

Regards,
Björn

--
BOFH excuse #415:

Maintenance window broken

Oct 12 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: bloodhound | last post by:
Hi, Problem with global.asa not firing 100% of the time. This error crops up several times throughout the day but if you wait a while and reload the page (could be 5 mins or an hour) it will...
10
by: Shuo Xiang | last post by:
Greetings: I know that variables declared on a stack definitely does not reside in heap space so there is only a very limited amount of stuff that you can store in the stack of a function. But...
6
by: Andrea Williams | last post by:
Where is the best place to put global variables. In traditional ASP I used to put all of them into an include file and include it in every page. Will the Global.aspx.cs do that same thing? ...
1
by: stewart | last post by:
I've got the standard SqlCacheDependency working just fine , ie. I've defined (and encrypted) the connectionStrings section in the web.config, and I've also defined an an sqlCacheDependency in the...
1
by: Dave | last post by:
Hello All, I'm trying to clarify how Python avoids byte by byte string comparisons most of the time. As I understand, dictionaries keep strings, their keys (hash values), and caches of their...
7
by: Jonathan Wood | last post by:
Can someone tell me how to compare two strings using a case-insensitive comparison. Ideally, I would prefer not to have to change a global setting or override any other interfaces. BTW, does...
4
by: Matt Colegrove | last post by:
I'm working on a web app that is published to a hosting service. I'm developing it on my local PC with VS 2005 and SQL Express. The hosting service DB is SQL Server 2000. I have two...
8
by: ben | last post by:
hello there, oh dear, oh dear. here's a non global array of strings: char *chararray = { "abc", "defgh", "ijklmop" }; how do i do that so chararray is global? what goes in a .h file and...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.