1.1 --- a/docs/syspython.txt Fri Nov 29 01:03:47 2013 +0100
1.2 +++ b/docs/syspython.txt Fri Nov 29 01:04:20 2013 +0100
1.3 @@ -124,6 +124,63 @@
1.4 if something:
1.5 storeattr(module.C, method, something)
1.6
1.7 +Local class or function definitions are also handled in a similar fashion to
1.8 +those at the module level, although there is no explicit __main__ function
1.9 +within each function.
1.10 +
1.11 +For example:
1.12 +
1.13 + def outer(x):
1.14 + if something:
1.15 + def inner(...):
1.16 + ...
1.17 + else:
1.18 + def inner(...):
1.19 + ...
1.20 + return inner
1.21 +
1.22 +This is represented as follows:
1.23 +
1.24 + def outer(x):
1.25 + def inner(...):
1.26 + ...
1.27 + def inner(...):
1.28 + ...
1.29 +
1.30 + if something:
1.31 + storelocal(inner, static(outer.inner))
1.32 + else:
1.33 + storelocal(inner, static("outer.inner#2"))
1.34 + return inner
1.35 +
1.36 +Where functions are dynamic - that is they have additional state associated
1.37 +with them, such as defaults (or potentially closures if supported) that are
1.38 +not static (such as constant values) - suitable objects must be created using
1.39 +references to such functions.
1.40 +
1.41 +For example:
1.42 +
1.43 + def outer(x):
1.44 + def inner(y, z=x):
1.45 + ...
1.46 + return inner
1.47 +
1.48 +This is represented as follows:
1.49 +
1.50 + def outer(x):
1.51 + def inner(__context__, y, z=x):
1.52 + localnames(__context__, y, z)
1.53 + ...
1.54 + storelocal(inner, makedynamic(static(outer.inner), x))
1.55 + return inner
1.56 +
1.57 +The special makedynamic invocation creates an object referring to the function
1.58 +and incorporating any specified defaults as attributes of that object. The
1.59 +function itself uses a special __context__ parameter that acts somewhat like
1.60 +the self parameter in methods: when invoked, the __context__ provides access
1.61 +to any default information that needs to be transferred to the local
1.62 +namespace.
1.63 +
1.64 Imports
1.65 -------
1.66