Home arrow Python arrow Page 9 - Python 101 (part 6): Hedgehogs, Pythons And Funky Chameleons

Flavour Of The Month - Python

This week, Python 101 discusses how to abstract out parts of yourPython code into reusable functions, add flexibility to them by allowingthem to accept different arguments, and make them return specific values.Also included: a discussion of variable scope and functions to help youwrite your own functions. Confused? All is explained within...

TABLE OF CONTENTS:
  1. Python 101 (part 6): Hedgehogs, Pythons And Funky Chameleons
  2. Cheating The Taxman
  3. Talking Movies
  4. Call Me Sometime
  5. Return To Me
  6. Tall, Dark And Handsome
  7. Arguing Your Case
  8. Enter The Funky Chameleon
  9. Flavour Of The Month
  10. Hip To Be Square
By: Vikram Vaswani, (c) Melonfire
Rating: starstarstarstarstar / 4
July 16, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
Let's now talk a little bit about the variables used within a function, and their relationship with variables in the main program. Unless you specify otherwise, the variables used within a function are local - that is, the values assigned to them, and the changes made to them, are restricted to the function space alone.

For a clearer example of what this means, consider this simple example:


#!/usr/bin/python # set a variable outside the function flavour = "tangerine" # alter the variable within the function def changeFlavour(): flavour = "raspberry" # before invoking function print "(before) Today's flavour is", flavour # invoke function changeFlavour() # after invoking function print "(after) Today's flavour is", flavour
And here's what you'll see:

(before) Today's flavour is tangerine (after) Today's flavour is tangerine
As you can see, assignment to a variable within a function does not alter the same variable outside the function...unless you declare it with the "global" keyword.

#!/usr/bin/python # set a variable outside the function flavour = "tangerine" # alter the variable within the function def changeFlavour(): global flavour flavour = "raspberry" # before invoking function print "(before) Today's flavour is", flavour # invoke function changeFlavour() # after invoking function print "(after) Today's flavour is", flavour
And now, when you run it,

(before) Today's flavour is tangerine (after) Today's flavour is raspberry
The "global" keyword tells Python that changes made to the variable should be applied globally, and should not remain localized to the function space alone.

Note, however, that the "global" keyword is only used when you plan to alter a global variable; if all you need to do is read a global variable, Python can access its value without any requirement to first declare it global.

#!/usr/bin/python # set a variable outside the function flavour = "tangerine" # use the variable within the function def whatFlavour(): print "Today's flavour is", flavour # invoke function whatFlavour()
In this case, even though the variable is declared outside the function, Python can still access (though not change) its value.

Today's flavour is tangerine


 
 
>>> More Python Articles          >>> More By Vikram Vaswani, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: