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

Call Me Sometime - 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
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
 

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: