SunQuest
 
       Python
  Home arrow Python arrow Page 8 - 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 - 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

    - 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