【发布时间】:2021-06-24 13:48:17
【问题描述】:
1- 是真的吗? 特定类的所有对象都有自己的数据成员,但共享成员函数,内存中只存在一个副本?
2- 以及为什么这段代码中init的地址相似:
class c:
def __init__(self,color):
print (f"id of self in __init__ on class is {id(self)}")
def test(self):
print("hello")
print (f"id of __init__ on class is {id(__init__)}")
a=c("red")
print(id(a.__init__))
print(id(a.test))
b=c("green")
b.test()
print(id(b.__init__))
print(id(b.test))
Output:
id of __init__ on class is 1672033309600
id of self in __init__ on class is 1672033251232
**1672028411200
1672028411200**
id of self in __init__ on class is 1672033249696
hello
**1672028411200
1672028411200**
【问题讨论】: