Python
  Home arrow Python arrow Page 4 - Python 101 (part 6): Hedgehogs, Python...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
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? 
PYTHON

Python 101 (part 6): Hedgehogs, Pythons And Funky Chameleons
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Python 101 (part 6): Hedgehogs, Pythons And Funky Chameleons - Call Me Sometime


    (Page 4 of 10 )

    Of course, defining a function is only half of the puzzle - for it to be of any use at all, you need to invoke it. In Python, as in Perl, PHP and a million other languages, this is accomplished by "calling" the function by its name, as I've done in the last line of the example above. Calling a user-defined function is identical to calling a built-in Python function like print() or type().

    It should be noted that Python will barf if you attempt to call a function before it has been defined - look what happens when I try the following example:



    #!/usr/bin/python # main program begins here print "Question: which is the greatest movie of all time?" # call the function greatMovie() # ask another question print "Question: which movie introduced the world to Luke Skywalker, Yoda and Darth Vader?" # call the function greatMovie() # define a function def greatMovie(): 'this prints the name of my favourite movie' print "Star Wars"
    Here's the output:

    Question: which is the greatest movie of all time? Traceback (innermost last): File "./test.py", line 8, in ? greatMovie() NameError: greatMovie
    Another important thing to remember about functions is that they're, at heart, objects...just like everything else in Python. Consequently, they can be assigned to other objects, passed as values or data structures, and otherwise treated as though they were any other Python object.

    The following example demonstrates this object-oriented behaviour, by assigning one function to another, and then using the new assignment to invoke the function.

    #!/usr/bin/python # define a function def greatMovie(): 'this prints the name of my favourite movie' print "Star Wars" # assign function as an object myFavouriteMovie = greatMovie print "Question: which is the greatest movie of all time?" # call function by its new name myFavouriteMovie()
    And the output is:

    Question: which is the greatest movie of all time? Star Wars

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


     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway