question on global variables
Harry George
hgg9140 at seanet.com
Wed Oct 9 23:27:24 EDT 2002
mongo57a at comcast.net writes:
> Somewhat new to Python here.....
>
> I want to change/use a value throughout my program - what I would normally
> refer to as a "global" variable.
>
> I see that there is a "global" command - and I assume its use would be
> global var_name. This I have coded (works) but I am unable to use it "global
> name var_name is not defined".
>
> TIA
>
>
mydata=0
print mydata --> 0
def myfunc1(x):
mydata=x
myfunc1(5)
print mydata --> 0
def myfunc2(x):
global mydata
mydata=x
myfunc2(5)
print mydata --> 5
That is, "global" allows globally defined data to be modified from
inside a function. Otherwise it is read only, and treated as a local
variable.
Of course, you should consider capturing "global" data in some object,
so you can isolate it if you need to do threads.
> -----------== Posted via Newsfeed.Com - Uncensored Usenet News ==----------
> http://www.newsfeed.com The #1 Newsgroup Service in the World!
> -----= Over 100,000 Newsgroups - Unlimited Fast Downloads - 19 Servers =-----
--
Harry George
hgg9140 at seanet.com
More information about the Python-list
mailing list