Python
  Home arrow Python arrow Page 9 - Python 101 (part 6): Hedgehogs, Pythons And Funky Chameleons
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PYTHON

Python 101 (part 6): Hedgehogs, Pythons And Funky Chameleons
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2001-07-16


    Table of Contents:
  • Python 101 (part 6): Hedgehogs, Pythons And Funky Chameleons
  • Cheating The Taxman
  • Talking Movies
  • Call Me Sometime
  • Return To Me
  • Tall, Dark And Handsome
  • Arguing Your Case
  • Enter The Funky Chameleon
  • Flavour Of The Month
  • Hip To Be Square

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Python 101 (part 6): Hedgehogs, Pythons And Funky Chameleons - Flavour Of The Month
    ( Page 9 of 10 )

    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
     

       

    PYTHON ARTICLES

    - 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
    - Sequences and Sets in Python
    - Python Expressions and Operators
    - Dictionaries, Variables and Statements in Py...
    - Data Types in Python
    - The Python Language
    - SSH with Twisted





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek