Observations on the three pillars of Python execution
Eric Snow
ericsnowcurrently at gmail.com
Fri Aug 5 03:20:24 EDT 2011
In Python, three types of objects have special syntax and mechanics
for their instantiation, during which a code object is generated:
modules, classes, and functions. Each has its own role to play and
the differences between them mostly reflect that. Here are some
observations (based on default behavior):
Object creation relative to code object creation:
(M) before
(C) after
(F) after
Object creation relative to code object execution:
(M) before
(C) after
(F) before
Object available during code object execution:
(M) no
(C) no
(F) no
Code object destiny:
(M) executed once at definition time, then discarded
(C) executed once at definition time, then discarded
(F) not executed at definition time, stored on the function
object for execution with each __call__() invocation.
Execution locals is available during code object execution:
(M) as globals()
(C) as locals()
(F) as locals()
Object namespace is execution locals of code object:
(M) yes
(C) more or less
(F) completely distinct
Unique syntax:
(M) 'import' statement
(C) 'class' statement
(F) 'def' statement
Mechanism triggered by respective syntax:
(M) import machinery (import.c/importlib)
(C) internal, but partially exposed via metaclasses and the
__build_class__ builtin
(F) internal, nothing exposed
Override capability:
(M) complete capability through builtin __import__ and PEP
302 import hooks
(C) partial capability, before code object execution through
metaclass __prepare__() and after through __build_class__()
and metaclass __call__(), __new__(), and __init__()
(F) no capability
Post instantiation modification capability:
(M) yes
(C) yes
(F) mostly (some attributes are read-only)
Mechanism to instantiate outside of respective unique syntax:
(M) imp.new_module(), types.ModuleType(), type(<module>)()
(C) type()
(F) types.FunctionType(), type(f)()
Type extensibility:
(M) Not relative to 'import' syntax (by default)
(C) Complete
(F) No
Name available during definition execution:
(M) as __name__
(C) only through metaclass __prepare__()
(F) through inspect.currentframe().f_code.co_name
Name available on object as __name__:
(M) yes
(C) yes
(F) yes
Corrections, additions, and comment are welcome.
-eric
More information about the Python-list
mailing list