Lexical Scoping in Python
·
Vivek H.
What is a closure?
- why is it helpful?
Python’s underyling memory structure for functions
- build up enough of an explanation that a language beginner can understand pyObjects,
- explain how in CPython, a PyFuncObject has a pointer to a PyObject of default values, keyword defaults, etc.
- https://github.com/python/cpython/blob/3db0a21f731cec28a89f7495a82ee2670bce75fe/Include/cpython/funcobject.h
A closure is stored as a variable (tuple of cell objects)
- What would you expect to occur naively?
What about in another familiar language, like java script?
How can you make this work?
Default parameters, where you fill in a value
Why does this work?
- Functions are also python objects! So, when you pass a default parameter, a copy of it at runtime becomes associated with the function object.
- what if the object is a list of lists?
- Does a default parameter make a deep copy?
- to do: run this experiment.
- Does a default parameter make a deep copy?