Training, Open Source computer languages

This is page http://www.wellho.net/forum/Programm ... -Ruby/Variable-scope-in-Python.html

Our email: info@wellho.net • Phone: 01144 1225 708225

 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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.

© WELL HOUSE CONSULTANTS LTD., 2024: 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