[Python-ideas] Tweaking closures and lexical scoping to include the function being defined

Jim Jewett jimjjewett at gmail.com
Fri Sep 30 16:27:36 CEST 2011

>> I don't *like*
>> boxing up my variables, but it still seems less offensive than a
>> global statement and the resulting side effects.

On Thu, Sep 29, 2011 at 4:26 PM, Devin Jeanpierre
<jeanpierreda at gmail.com> asked:

> How is that?

Normally, things at the module level can only be modified by other
lines at the module level, or by monkey-patching.  The global
statement changes that.

    >>> x=5   # Safe ... no other assignments on the far left ...
    >>> class T:
                x=4    # OK, so there is a class level variable
                def __init__(self):
                    global x    # which, alas, isn't what gets used

    >>> x
    >>> T()
    <__main__.T object at 0x0137A270>
    >>> x

Wait -- how did x get changed?  *I* didn't change it, did I?


More information about the Python-ideas mailing list