>>> def sum(x, y):
... def do_it():
... return x + y
... return do_it
...
>>> a = sum(1, 3)
>>> a
<function do_it at 0xb772b304>
>>> a()
4
Is this what you were looking for? It’s called a closure.
More Related Contents:
- Local variables in nested functions
- Best way to return multiple values from a function? [closed]
- Single quotes vs. double quotes in Python [closed]
- Why aren’t python nested functions called closures?
- Dictionaries and default values
- What are the most common Python docstring formats? [closed]
- Forced naming of parameters in Python
- Python `if x is not None` or `if not x is None`? [closed]
- Python exception chaining [duplicate]
- How to break a line of chained methods in Python?
- In Python, is it better to use list comprehensions or for-each loops?
- Why use def main()? [duplicate]
- Should I use `import os.path` or `import os`?
- python close file descriptor question
- Python import mechanics
- Passing argument from Parent function to nested function Python
- Alternatives for returning multiple values from a Python function [closed]
- Creating an empty list in Python
- What does nonlocal do in Python 3?
- Importing modules in Python – best practice
- How do nested functions work in Python?
- Is making in-place operations return the object a bad idea?
- identity versus equality for None in Python
- Tool to convert Python code to be PEP8 compliant
- Python coding standards/best practices [closed]
- Line continuation for list comprehensions or generator expressions in python
- Attributes initialization/declaration in Python class: where to place them?
- How can we force naming of parameters when calling a function?
- Length of longest word in a list
- Why does PEP-8 specify a maximum line length of 79 characters? [closed]