|
Sharing variables with functions, but keeping them local too - Python
""" One of the big issues with any programming language is how in shares (scopes) variables between blocks of code. On one hand there's a desire to have variables easily accessible without a lot of passing around. On the other hand there's the need to keep information in vary different parts of you code apart, with a variable name you happen to have used twice not resulting in a memory location being shared between two pieces of code which are otherwise independent of each other.
With the notable exception of Perl, languages "scope" variables to the subroutine / procedure / method / proc / macro in which they are defined, unless you take other specific action such as declaring them global.
This piece of Python code - a rather messy example which resulted from a practical demonstration earlier today - shows examples of many facets of variable sharing to defined functions, and a couple of surprises too! """
# This first is a variable in the main code only.
# There is a different first in the function "one"
# second is in the main code
# As it is used READ ONLY in the function, it is shared there
first = 1
second = 14
def one():
first = 4 + second
first *= 2
print first
result = first+3
# Referring to a variable in the functions own name space
# is broadly equivalent to it being "static" - it is visible
# outside, and retained for next time the function is called
one.latest = result
return result, first, second
# A Lambda is a quick way of defining a function
two = lambda john: john + 2
# A function is an object, so you can easily give
# it another reference (i.e. a second name!)
three = two
print first
# A function returns one object - but if that object is a
# tuple, then it can be saved as multiple variables (objects)
zig, zag, zog = one()
print zig
print zag
print zog
print first
print second
# Swapping over two values in Python
# No need for a temporary variable
# (or rather - Python provides the temporary tuple!)
first, second = second, first
print first
print second
dis = two(first)
print dis
dat = three(dis)
print dat
print one.latest
""" Here are the results if running that!
1
36
39
36
14
1
14
14
1
16
18
39
"""
""" Using the ''' notation, the whole of this article is a working Python program which you can simply cut and paste and run for yourself!"""(written 2008-09-09 23:48:12)
Associated topics are indexed under Y105 - Python - Functions, Modules and Packages
Some other Articles
Refactoring - a PHP demo becomes a production pageWhich country does a search engine think you are located in?All the pieces fall into place - hotel and coursesThe road ahead - Python 3Sharing variables with functions, but keeping them local too - PythonLooking for a value in a list - PythonPython 2 to Python 3 / Python 3000 / Py3kHowto - write and manage a news box on your web pagePicturing the rainWhat is running on your network? (tcl and expect)
|
1891 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38 at 50 posts per page
This is a page archived from The Horse's Mouth at
http://www.wellho.net/horse/ -
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.
Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).
|
|