| |||||||||||
Variable scope in Python Posted by admin (Graham Ellis), 29 May 2004 The scope of variables (how widely they're seen, when they are created and destroyed, and the mechanisms through which the same variable can share several names) is one of tose topics that comes up in any programming language. Here's an example in Python that shows some of the options:Code:
somevar is a declared in the function as a global variable, so that it's the same variable as somevar in the main code. Therefore it's initialised to 27, and incremented by the function to 29 which is the value printed out. pval is initialised to 16 and is only used under that name in the main code. However, it's passed into the function where it's accessed under the name f_in, and where it gets increased by 14, so that the value 30 is printed out. Note that Python calls "by reference" rather than by copying the contents into the function. res is a local variable used purely within the function There are two different variables called another. One is in the main code and the other is in the function. They do not refer to the same information / object in any way - they just happen to have the same name. The variable name is rather like a forename, so the two anothers are as different as John Smith and John Jones. demo.tom is a variable within the demo name, and we're able to reference it directly elsewhere too. It's initialised to 16 in the demo function, and printed out (showing that value) in the main code. Posted by cloughie (cloughie), 19 December 2007 You state that the pval variable is passed in 'by reference', but changing the value within the 'demo' function wouldn't actually change the value of pval outside of the function ... so I'm confused.Also, what's the benefit of declaring a variable as 'demo.tom' over the global variable 'somevar'. They share similar facets as far as I can tell. I must say, I do find the scope of variables confusing in certain situations. Normally I play about with the code until I eventually (after much frustration) get it working. Perhaps if I understood it a little better ... Regards, Posted by admin (Graham Ellis), 20 December 2007 I'm really rushing around today, so please fogive a slightly shorter answer than normal.The big use of a variable such as demo.tom within the demo namespace is that you can very cleraly see that it's a variable that's set up and relates to that modules / namespace and won't conflict with other variables of the same name in othe modules / namespaces. If you have a single global "copyright" variable, but then several modules each of which had its own copyright, you would have a problem. But with a copyright variable in each module, problem solved. This page is a thread posted to the opentalk forum
at www.opentalk.org.uk and
archived here for reference. To jump to the archive index please
follow this link.
|
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |