Python
  Home arrow Python arrow Page 4 - Python Parameters, Functions and Arguments
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 Parameters, Functions and Arguments
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 1
    2008-10-30


    Table of Contents:
  • Python Parameters, Functions and Arguments
  • Attributes of Function Objects
  • Other attributes of function objects
  • Kinds of arguments

  • 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 Parameters, Functions and Arguments - Kinds of arguments
    ( Page 4 of 4 )

    Arguments that are just expressions are known as positional arguments. Each positional argument supplies the value for the parameter that corresponds to it by position (order) in the function definition.

    In a function call, zero or more positional arguments may be followed by zero or more named arguments, each with the following syntax:

      identifier=expression

    The identifier  must be one of the parameter names used in the def statement for the function. The expression  supplies the value for the parameter of that name. Most built-in functions do not accept named arguments, you must call such functions with positional arguments only. However, all normal functions coded in Python accept named as well as positional arguments, so you may call them in different ways.

    A function call must supply, via a positional or a named argument, exactly one value for each mandatory parameter, and zero or one value for each optional parameter. For example:

      def divide(divisor, dividend):
         
    return dividend // divisor
      print divide(12, 94)             # prints: 7
      print divide(dividend=94,
    divisor=12)                        # prints: 7

    As you can see, the two calls to divide are equivalent. You can pass named arguments for readability purposes whenever you think that identifying the role of each argument and controlling the order of arguments enhances your code's clarity.

    A common use of named arguments is to bind some optional parameters to specific values, while letting other optional parameters take default values:

      def f(middle, begin='init', end='finis'):
         
    return begin+middle+end
      print f('tini', end='')          # prints: inittini

    Thanks to named argument end='', the caller can specify a value, the empty string '',  for f's third parameter, end, and still let f's second parameter, begin, use its default value, the string 'init'.

    At the end of the arguments in a function call, you may optionally use either or both of the special forms *seq and **dct. If both forms are present, the form with two asterisks must be last. *seq  passes the items of seq  to the function as positional arguments (after the normal positional arguments, if any, that the call gives with the usual syntax). seq may be any iterable. **dct  passes the items of dct  to the function as named arguments, where dct must be a dictionary whose keys are all strings. Each item's key is a parameter name, and the item's value is the
    argument's value.

    Sometimes you want to pass an argument of the form *seq  or **dct when the parameters use similar forms, as described earlier in "Parameters" on page 71. For example, using the function sum_args defined in that section (and shown again here), you may want to print the sum of all the values in dictionary d . This is easy with *seq :

      def sum_args(*numbers):
         
    return sum(numbers)
      print sum_args(*d.values())

    (Of course, in this case, print sum(d.values()) would be simpler and more direct!)

    However, you may also pass arguments of the form *seq  or **dct  when calling a function that does not use the corresponding forms in its parameters. In that case, of course, you must ensure that iterable seq  has the right number of items, or, respectively, that dictionary dct  uses the right names as its keys; otherwise, the call operation raises an exception.

    Please check back next week for the conclusion to this article.



     
     
    >>> More Python Articles          >>> More By O'Reilly Media
     

       

    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