# HG changeset patch # User Paul Boddie # Date 1385683460 -3600 # Node ID 94cb3ad3225a79736b9b41a44d83187173cb1b58 # Parent fa655857f2bae346e5b09e12e5331c80292c368a Added some documentation about defaults and dynamic functions. diff -r fa655857f2ba -r 94cb3ad3225a docs/syspython.txt --- a/docs/syspython.txt Fri Nov 29 01:03:47 2013 +0100 +++ b/docs/syspython.txt Fri Nov 29 01:04:20 2013 +0100 @@ -124,6 +124,63 @@ if something: storeattr(module.C, method, something) +Local class or function definitions are also handled in a similar fashion to +those at the module level, although there is no explicit __main__ function +within each function. + +For example: + + def outer(x): + if something: + def inner(...): + ... + else: + def inner(...): + ... + return inner + +This is represented as follows: + + def outer(x): + def inner(...): + ... + def inner(...): + ... + + if something: + storelocal(inner, static(outer.inner)) + else: + storelocal(inner, static("outer.inner#2")) + return inner + +Where functions are dynamic - that is they have additional state associated +with them, such as defaults (or potentially closures if supported) that are +not static (such as constant values) - suitable objects must be created using +references to such functions. + +For example: + + def outer(x): + def inner(y, z=x): + ... + return inner + +This is represented as follows: + + def outer(x): + def inner(__context__, y, z=x): + localnames(__context__, y, z) + ... + storelocal(inner, makedynamic(static(outer.inner), x)) + return inner + +The special makedynamic invocation creates an object referring to the function +and incorporating any specified defaults as attributes of that object. The +function itself uses a special __context__ parameter that acts somewhat like +the self parameter in methods: when invoked, the __context__ provides access +to any default information that needs to be transferred to the local +namespace. + Imports -------