Are decorators really that different from metaclasses...
Paul Morrow
pm_mon at yahoo.com
Tue Aug 31 16:28:20 EDT 2004
Steven Bethard wrote:
> Paul Morrow <pm_mon <at> yahoo.com> writes:
>
>>The assignments to __xxx__ variables (immediately following the
>>docstring, if present) would occur in the namespace of the object
>>(function/method) being defined. The assignments would not cause them
>>to change namespaces.
>
>
>
> What you're suggesting is that given:
>
> def f1():
> __author__ = 'Steve'
>
> and
>
> def f2():
> author = 'Steve'
>
> in f1, the assignment to __author__ occurs in the function's namespace, but in
> f2, the assignment to author occurs in the local namespace. Clearly then, the
> __xxx__ format (if at the beginning of a function) changes the namespace to
> which an assignment applies. How is this not causing assignments to change
> namespaces?
>
def f3():
"""I am a docstring."""
__author__ = 'Steve'
y = 10
When the interpreter processes a function def, it places the name of the
function and its docstring into the namespace of the function being
defined. So instead of stopping at the docstring, it would look ahead
to see if there were any subsequent " __xxx__ = " statements, and if so,
would evaluate them in the same namespace [*]. After the def was
finished, the function's namespace would include not only its name and
docstring, but an attribute for each __xxx__ assignment as well.
Later, when the function is executed, its local, global, and built-in
namespaces would be exactly what they are now, except that its local
namespace would not include any __xxx__ assignments processed when the
def statement was executed.
So in f3 above, its local namespace would include 'y' only.
Paul
[*] The namespace of the function being defined; the same place it put
the function's name and docstring.
More information about the Python-list
mailing list