猿问

python中为什么方法在类中定义的,但是内存地址却不同??

>>> class Student(): 

def __init__(self,name,score):

self.name=name
self.score=score

def print_score(self):

print ('%s:%s'%(self.name,self.score))

>>> bart=Student('王大治',23)
>>> id(Student)
54456368  //这里是类的内存地址
>>> id(bart)
5319568  //这个是实例内存地址
>>> id(bart.print_score)
4298912 //这个是方法的内存地址。这个方法,和实例。类的内存地址都不同,为什么呢?
>>> id(bart.score)
503371152 
>>> k=23
>>> id(k)
503371152

 从上述看,变量,实例,方法 ,类,的内存地址都不同,如果在内存中,那是怎么去连接运行的呢?


慕丝7291255
浏览 1020回答 1
1回答

慕沐林林

虽然不懂什么叫 Python的内存问题,下面是help(id)的结果:>>> help(id)Help on built-in function id in module builtins:id(obj, /)Return the identity of an object.This is guaranteed to be unique among simultaneously existing objects.---获取的identity就是唯一的。类也是个内存中的对象,类对象,Python中一切皆对象。 “连接运行”,WHY?
随时随地看视频慕课网APP

相关分类

Python
我要回答