The Python documentation on classes says that when a class definition is entered, a new namespace is created, and used as the local scope.
Any assignments to the variables go in this local space. Function definitions bind the name of the new function in this local scope.
Then I executed this simple piece of code in Python:
class C:
next_serial = 1337
def __init__(self, x, y):
self.x = x
self.y = y
self.z = next_serial
next_serial += 1
c1 = C('ABC','XYZ')
print(c1.x)
print(c1.y)
print(c1.z)
It failed with UnboundLocalError: local variable 'next_serial' referenced before assignment
Now as per the documentation Class C created a new namespace which acts as a local scope where variable next_serial is bound to int object 1337 and function init is bound to the definition of the function in the class.
Why did Python not treat this local scope as an enclosing scope for the function init?