From 40cc55d040d9d3efc0b24286fc650be1b473a25f Mon Sep 17 00:00:00 2001 From: Joe Kim Date: Sat, 3 Nov 2018 21:56:33 -0400 Subject: [PATCH] Added content : defining function inside functions (#20854) --- guide/english/python/defining-functions/index.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/guide/english/python/defining-functions/index.md b/guide/english/python/defining-functions/index.md index 189a1a61023..bb847c1bc03 100644 --- a/guide/english/python/defining-functions/index.md +++ b/guide/english/python/defining-functions/index.md @@ -22,3 +22,19 @@ The [`def`](https://docs.python.org/3/reference/compound_stmts.html#def) keyword The first statement of the function body can optionally be a string literal; this string literal is the function's documentation string, or [docstring](https://www.python.org/dev/peps/pep-0257/) (More about docstrings can be found in the section Documentation Strings). Some tools use docstrings to automatically produce online or printed documentation or to let the user interactively browse through code. It's good practice to include docstrings in code that you write, so try to make a habit of it. No closing statement is required for the function (eg something similar to Ruby's END), all code that is indented following the opening definition is within scope of the function. + + +One interesting thing about functions in Python... +We can define a function inside another function! + + >>> def foo(): + ... def bar(): + ... print('BAR') + ... print('FOO') + ... bar() + >>> # Now call the function we just defined: + ... foo() + FOO + BAR + +If a function is defined inside another function, the inner function can only be called inside the function in which it was defined.