As shown in the code, a parent class ClassA
is established, which has a variable var1
, and a subclass ClassB
inherits the parent class, which has a variable var2
. As a result, the addresses of var1
and var2
are the same, why is this?
As a result, whether I assign a value to var1
or var2
, the value of both is the same. What is the reason for this?
My code is as follows:
class ClassA:
def __init__(self):
self.var1: float = 0
class ClassB(ClassA):
def __init__(self):
super(ClassB, self).__init__()
self.var2: float = 0
print(id(self.var1) == id(self.var2))
a = ClassB()
The result I get:
True
My expected result:
False
Tried many times, only this can achieve my goal:
class ClassA:
def __init__(self):
self.var1: float = float('nan')
class ClassB(ClassA):
def __init__(self):
super(ClassB, self).__init__()
self.var2: float = float('nan')
print(id(self.var1) == id(self.var2))
a = ClassB()
False