Home arrow Python arrow Page 4 - Python Parameters, Functions and Arguments

Kinds of arguments - Python

In this eighth part of a nine-part series on the Python programming language, we focus strongly on two aspects of functions: parameters and arguments. This article is excerpted from chapter four of the book Python in a Nutshell, Second Edition, written by Alex Martelli (O'Reilly; ISBN: 0596100469). Copyright 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

  1. Python Parameters, Functions and Arguments
  2. Attributes of Function Objects
  3. Other attributes of function objects
  4. Kinds of arguments
By: O'Reilly Media
Rating: starstarstarstarstar / 3
October 30, 2008

print this article



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:


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

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: