Book Image

Python Essentials

By : Steven F. Lott
Book Image

Python Essentials

By: Steven F. Lott

Overview of this book

Table of Contents (22 chapters)
Python Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Nested function definitions


We can include anything inside a function definition, even another function definition. When we look at decorators in Chapter 13, Metaprogramming and Decorators, we'll see cases of a function which includes a nested function definition.

We can include import statements within a function definition. An import statement is only really executed once. There's a global collection of imported modules. The name, however, would be localized to the function doing the import.

The general advice is given in the Zen of Python poem by Tim Peters:

Flat is better than nested.

We'll generally strive to have functions defined in a relatively simple, flat sequence. We'll avoid nesting unless it's truly required, as it is when creating decorators.