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

Enter The Funky Chameleon - 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...

  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


The order in which arguments are passed to a function is important - the following example assumes that the name is passed as the first argument, and the age as the second.

>>> def User(name, age): ... print "Hello, " + name + ". How strange that we are both " + age + " years old." ... >>> User("Funky Chameleon", "23") Hello, Funky Chameleon. How strange that we are both 23 years old. >>>
If you get the order wrong, you may get unexpected results.

>>> User("23", "Funky Chameleon") Hello, 23. How strange that we are both Funky Chameleon years old. >>>
However, it's possible to override this behaviour in the function call itself, by specifying the arguments as name-value pairs.

>>> User(age="23", name="Funky Chameleon") Hello, Funky Chameleon. How strange that we are both 23 years old. >>>
Additionally, you can specify certain arguments to be optional, by setting default values for them in the function definition. Consider the following example, which requires two arguments, a URL and a language to display it in. If the language argument is omitted, a default value is used.

>>> def displayURL(url, lang="English"): ... print "Displaying URL <" + url + "> in " + lang ... >>> displayURL("http://www.melonfire.com", "Spanish") Displaying URL in Spanish >>> displayURL("http://www.melonfire.com") Displaying URL in English >>>
Note, however, that the list of optional arguments must follow, not precede, the list of required arguments in the function definition.

All these examples have one thing in common - the list of arguments is fixed. Look what happens if you pass an extra argument to the User() function above,

>>> User("Harry the Hedgehog", "2", "male") Traceback (innermost last): File "", line 1, in ? TypeError: too many arguments; expected 2, got 3 >>>
or miss out on a parameter when calling the function.

>>> User("Harry the Hedgehog") Traceback (innermost last): File "", line 1, in ? TypeError: not enough arguments; expected 2, got 1 >>>
To handle situations such as there, Python functions come with a built-in secret weapon - the *variable argument. Consider the following function definition:

>>> def User(name, age, *more): ... print "Name:", name ... print "Age:", age ... for x in more: ... print "Additional argument:", x ...
In this case, extra arguments passed to the function will be stored in the variable "more" as a tuple; these arguments can then be extracted and processed using standard tuple operators or methods.

>>> User("Harry the Hedgehog", "2", "male") Name: Harry the Hedgehog Age: 2 Additional argument: male >>> User("Harry the Hedgehog", "2", "male", "angry", "lives next door") Name: Harry the Hedgehog Age: 2 Additional argument: male Additional argument: angry Additional argument: lives next door >>>
Here's an example which demonstrates how this works.

>>> def Product(*numlist): ... product = 1; ... for x in numlist: ... product = product * x ... return product ... >>>
In this case, you can call the Product() function with as many arguments as you like; the "for" loop will iterate through the "numlist" tuple and multiply each one by the previous result.

>>> Product(2, 3) 6 >>> Product(2) 2 >>> Product(2, 10, 10, 1) 200 >>> Product(2, 10, 10, 6326, 236237238923490234) OverflowError: integer literal too large >>>
Oh, well...

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: