Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
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:
def demo (f_in):
       global somevar  # shared with main code
       demo.tom = 16   # An attribute accessible from main code
       somevar += 1
       another = 12    # A local variable, independent of main code
       res = f_in+14   # Value passed in (f_in)
       return res

somevar = 27    # accessed in function via global
another = 17    # not accessed in function
pval = 16       # accessed in function via parameter

print demo(pval)

print demo.tom  # function attribute
print somevar
print another


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.


Visitor Ranking 4.0 (5=excellent, 1=poor)

edit your own (not yet published) comments

Average page ranking - 4.0

© WELL HOUSE CONSULTANTS LTD., 2022: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho