【发布时间】:2021-08-21 04:10:21
【问题描述】:
一个简短的继承示例:
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
class Student(Person):
def __init__(self, fname, lname):
super().__init__(fname, lname)
print(type(super()))
现在输入Student("test", "name") 将导致<class 'super'> 被打印到控制台。我不熟悉这种格式。当我执行type(int) 时,我看到类型为type,而不是<class 'int'>。有人可以解释这里发生了什么吗?
【问题讨论】:
-
试试看
type(int())给你什么 -
@bdbd 哦,谢谢。那么这到底是什么意思呢?单独输入
int()只会返回 0,大概是因为这是默认值。那么为什么type(int())不返回int,因为int()的计算结果是一个int? (int() + 2工作得很好,所以看起来int()正在返回一个 int 而不是别的东西) -
“那么为什么 type(int()) 不返回 int” - 它确实如此。
-
如前所述,它确实返回
int,而int实际上是class,因此您尝试使用type(int)打印类定义的类型,以及类实例与type(int()):) -
我也很好奇为什么
type(int)返回type而不是class,但这里有一些历史记录:stackoverflow.com/questions/4162578/…
标签: python class oop inheritance super