if you do the following:
>>> class Outer:
... class Inner:
... nb = 0
... def __init__(self):
... Outer.Inner.nb += 1
... def test(self):
... t = self.Inner()
... print(t.nb)
...
>>> o = Outer()
>>> o.test()
1
>>> o.test()
2
>>> o.test()
3
you're creating a class variable that will be bound the the class Outer.Inner
and
each time you modify it, it changes for all instances of that class.
My purpose is for every OuterClass instance to create a class of NestedClass. So I want the size to be different for every OuterClass instance.
If you want to have that variable dependant on each Outer
instance, you need
to create a reflexion to Outer
on Inner
:
>>> class Outer:
... def __init__(self):
... self.nb = 0
... class Inner:
... def __init__(self, o):
... o.nb += 1
... def test(self):
... t = self.Inner(self)
... print(self.nb)
...
>>> o = Outer()
>>> o.test()
1
>>> o.test()
2
>>> o.test()
3
>>> o2 = Outer()
>>> o2.test()
1
>>> o2.test()
2
>>> o2.test()
3
>>>
that way the variable is an instance member for each Outer
instance, and modified
by every Inner
instance.