class Box1:
def setDimension1(self, width1, height1, depth1):
self.width1 = width1
self.height1 = height1
self.depth1 = depth1
def getVolume1(self):
return self.width1 * self.height1 * self.depth1
b = Box1()
b.setDimension1(10, 20, 30)
print(b.getVolume1())
print(type(Box1))
print(type(b))
print(type(b.setDimension1))
代码如上, 疑问如下. 不影响日常写代码, 但和第一直觉不符, 有点好奇.
print(type(Box1)) 类型是 <class 'type'> 这里的type怎么理解呢? type类是什么类?
print(type(b)) 类型是 <class '__main__.Box1'> 而这里b的类型应该是对象? 实例化类, 应该得到对象?
type 就是“类型”。Box1 代表了一个类型。其它属于类型的还有
int
,float
,str
,list
,dict
....b 是 Box1 的对象,所以它的类型是 Box1 。类比一下,
1
是int
的对象,type(1)
是int
。