python 面向对象的小疑问?

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的类型应该是对象? 实例化类, 应该得到对象?
阅读 1.1k
1 个回答

type 就是“类型”。Box1 代表了一个类型。其它属于类型的还有 int, float, str, list, dict ....

b 是 Box1 的对象,所以它的类型是 Box1 。类比一下,1int 的对象,type(1)int

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题