python中的实例方法共享
1-这是真的吗? 特定类的所有对象都有自己的数据成员,但共享成员函数,内存中只存在一个副本? 2-以及为什么此代码中的init的地址类似:python中的实例方法共享,python,methods,instance,Python,Methods,Instance,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")
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**
init = a.__init__
test = a.test
print(id(init))
print(id(test))
你会得到不同的身份证。将方法分配给变量可以防止内存被重用
init = a.__init__
test = a.test
print(id(init))
print(id(test))
你会得到不同的身份证。将方法分配给变量可以避免重复使用内存。您对我的第一个问题有答复吗?我已经更新了答案。非常感谢。你能给我介绍一个关于这个主题的很好的参考资料,学习边界方法和相关主题吗?谷歌“python边界方法”:你对我的第一个问题有回应吗?我已经更新了答案。非常感谢。你能给我介绍一个关于这个主题的很好的参考资料,学习边界方法和相关主题吗?谷歌“python边界方法”: