Closure/method definition question (delete 'for Python 2.7')

Terry Reedy tjreedy at udel.edu
Mon Mar 10 14:59:10 EDT 2014


On 3/10/2014 1:27 PM, Brunick, Gerard:(Constellation) wrote:

> class Test(object):
>      x = 10
>
>      def __init__(self):
>          self.y = x
>
> t = Test()
> ---
>
> raises
>
> NameError: global name 'x' is not defined.
>
> in Python 2.7.

In Python, period.

> I would assume that when __init__ is being defined,
> it is just a regular old function

Right. It is an attribute of the class much like other object. This is 
more obvious and more consistent in Python 3. In Python 3, these two 
code snippets have the same effect.

class C:
   def f(self): return self.a
g = C.f  # in2.x, C.f.im_func

def g(self): return self.a
class C: pass
C.f = g

This is possible because functions are only loosely coupled to the class 
they are defined in, through the public attribute mechanism. This is 
handy for testing. There is no private namespace tie.  As long as self.a 
exists, g could function even if the class C object were deleted or 
inaccessible.

A function that accesses module globals *does* have a private namespace 
tie to the module it is defined in (lexical scoping), and this must be 
accounted for when testing or otherwise using it in another module -- 
perhaps by altering the original module.

> and x is a variable in an outer scope,

Non-global 'outer scope' is peculiar to lexically nested functions.  Any 
nonlocal access ties a function to its outer function through private 
internal references.

-- 
Terry Jan Reedy




More information about the Python-list mailing list