Home arrow Python arrow Page 2 - Nested Functions in Python

Nested functions and nested scopes - Python

In this final part of a nine-part series that focuses on Python, you will learn about namespaces, nested functions, and more. It 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.

TABLE OF CONTENTS:
  1. Nested Functions in Python
  2. Nested functions and nested scopes
  3. Generators
  4. Generators in Python 2.5
By: O'Reilly Media
Rating: starstarstarstarstar / 4
November 06, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

A def statement within a function body defines a nested function, and the function whose body includes the def is known as an outer function to the nested one. Code in a nested function's body may access (but not rebind) local variables of an outer function, also known as free variables of the nested function.

The simplest way to let a nested function access a value is often not to rely on nested scopes, but rather to explicitly pass that value as one of the function's arguments. If necessary, the argument's value can be bound when the nested function is defined by using the value as the default for an optional argument. For example:

  def percent1(a, b, c):
     
def pc(x, total=a+b+c): return (x*100.0) / total
     
print "Percentages are:", pc(a), pc(b), pc(c)

Here's the same functionality using nested scopes:

  def percent2(a, b, c):
     
def pc(x): return (x*100.0) / (a+b+c)
      print "Percentages are:", pc(a), pc(b), pc(c)

In this specific case, percent1 has a tiny advantage: the computation of a+b+c  happens only once, while percent2's inner function pc repeats the computation three times. However, if the outer function rebinds its local variables between calls to the nested function, repeating the computation can be necessary. It's therefore advisable to be aware of both approaches, and choose the most appropriate one case by case.

A nested function that accesses values from outer local variables is also known as a closure. The following example shows how to build a closure:

  def make_adder(augend):
      def add(addend):
          return addend+augend
      return add

Closures are an exception to the general rule that the object-oriented mechanisms covered in Chapter 5 are the best way to bundle together data and code. When you need specifically to construct callable objects, with some parameters fixed at object construction time, closures can be simpler and more effective than classes. For example, the result of make_adder(7) is a function that accepts a single argument and adds 7 to that argument. An outer function that returns a closure is a "factory" for members of a family of functions distinguished by some parameters, such as the value of argument augend  in the previous example, and may often help you avoid code duplication.

lambda Expressions

If a function body is a single return expression statement, you may choose to replace the function with the special lambda expression form:

  lambda parameters: expression

A lambda expression is the anonymous equivalent of a normal function whose body is a single return statement. Note that the lambda syntax does not use the return keyword. You can use a lambda expression wherever you could use a reference to a function. lambda can sometimes be handy when you want to use a simple function as an argument or return value. Here's an example that uses a lambda expression as an argument to the built-in filter function (covered in filter on page 161):

  aList = [1, 2, 3, 4, 5, 6, 7, 8, 9]
  low = 3
  high = 7
  filter(lambda x, l=low,
h=high: h>x>l, aList)         # returns: [4, 5, 6]

As an alternative, you can always use a local def statement that gives the function object a name. You can then use this name as the argument or return value. Here's the same filter example using a local def statement:

  aList = [1, 2, 3, 4, 5, 6, 7, 8, 9]
  low = 3
  high = 7
  def within_bounds(value, l=low, h=high):
      return h>value>l
  filter(within_bounds, aList)       # returns: [4, 5, 6]

While lambda can occasionally be useful, many Python users prefer def, which is more general, and may make your code more readable if you choose a reasonable name for the function.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

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