Go Back   Computer Forums > General Computing > Programming
Join Computer forums Today

Thread Tools Search this Thread Display Modes
Old 04-30-2013, 07:48 PM   #1
Baseband Member
palermo's Avatar
Join Date: Jan 2013
Location: United States
Posts: 81
Default Python: Global Variables?

I've been reading Think Python and I'm having trouble with understanding what a global variable is. Can anyone help me understand it or point me to a better Python study guide? Thank you.

palermo is offline   Reply With Quote
Old 05-02-2013, 05:04 AM   #2
Site Team
root's Avatar
Join Date: Mar 2004
Posts: 7,872
Default Re: Python: Global Variables?

in the context of programming..

a global variable is one that is accessible outside of a specific sub routing.


int number
in main()
...Main function...

int subfunction()
...sub function code...

in this example the variable called number is available to both the main function and the sub function. writting print number in either will show the contents of that variable.

int main ()
int letter
int number;
...more main code...
int subfunction()
int number
...more code...

here the variables are declared inside the functions, and thus local to the functions. the variable called letter only appears inside the main function and you can't use it in the subfunction.

the variable called number appears in both the main and the subfunction. but crucially it's a local variable in each case, whilst it has the same name, underneath the program it has a different memory reference and has/can have different data stored within it.
I didn’t fight my way to the top of the food chain to be a vegetarian…
Im sick of people saying 'dont waste paper'. If trees wanted to live, they'd all carry guns.
"The inherent vice of capitalism is the unequal sharing of blessings; The inherent vice of socialism is the equal sharing of miseries."
root is offline   Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

All times are GMT -5. The time now is 01:31 PM.

Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2016, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0