Added content : defining function inside functions (#20854)

pull/20856/head^2
Joe Kim 2018-11-03 21:56:33 -04:00 committed by Niraj Nandish
parent 0046c7601e
commit 40cc55d040
1 changed files with 16 additions and 0 deletions

View File

@ -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. 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. 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.