Share object only between nested classes
Question:
First of all I know that the code below is ridiculous and can be a simple loop but I want to understand if/how I can do some things in Nested Classes:
class OuterClass(object):
counter = 0 # Class variable
def __init__(self, v):
super(OuterClass, self).__init__()
self.values = v # Instance variable
self.run()
def run(self):
for v in values:
self.NestedClass(v) # can I bound a nested class to the outer?
print self.NestedClass.size
class NestedClass(object):
size = 0 # Nested Class variable
def __init__(self, value):
super(NestedClass, self).__init__() # NameError: global name 'NestedClass' is not defined
NestedClass.size += 1 # Nested Instance variable
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.
How can I fix the last two lines:
super(NestedClass, self).__init__()
NestedClass.size += 1
- If I write
self.NestedClass.size
it will try find a the NestedClass inside itself and throw an error.
- If I write
OuterClass.NestedClass.size
it will be bound to the outer class and the size will be the same object among OuterClass instances (I don’t want this).
- I also tried
OuterClass.self.NestedClass.size
with AttributeError: type object 'OuterClass' has no attribute 'self'
Answers:
… the size will be the same object among OuterClass instances (I don’t want this).
As long as size
is a class-member of NestedClass
, it only exists once. This is because there is only one class NestedClass
, and size
is a member of that class.
If you want a size
member for each instance, well, that’s what instance-members are for…
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.
First of all I know that the code below is ridiculous and can be a simple loop but I want to understand if/how I can do some things in Nested Classes:
class OuterClass(object):
counter = 0 # Class variable
def __init__(self, v):
super(OuterClass, self).__init__()
self.values = v # Instance variable
self.run()
def run(self):
for v in values:
self.NestedClass(v) # can I bound a nested class to the outer?
print self.NestedClass.size
class NestedClass(object):
size = 0 # Nested Class variable
def __init__(self, value):
super(NestedClass, self).__init__() # NameError: global name 'NestedClass' is not defined
NestedClass.size += 1 # Nested Instance variable
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.
How can I fix the last two lines:
super(NestedClass, self).__init__()
NestedClass.size += 1
- If I write
self.NestedClass.size
it will try find a the NestedClass inside itself and throw an error. - If I write
OuterClass.NestedClass.size
it will be bound to the outer class and the size will be the same object among OuterClass instances (I don’t want this). - I also tried
OuterClass.self.NestedClass.size
withAttributeError: type object 'OuterClass' has no attribute 'self'
… the size will be the same object among OuterClass instances (I don’t want this).
As long as size
is a class-member of NestedClass
, it only exists once. This is because there is only one class NestedClass
, and size
is a member of that class.
If you want a size
member for each instance, well, that’s what instance-members are for…
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.