Python
  Home arrow Python arrow Page 8 - 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 - Enter The Funky Chameleon
    ( Page 8 of 10 )

    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
     

       

    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 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek