以下Python 3.5代码:
My type is <class '__main__.Derived'>
My type is <class '__main__.Derived'>
打印:
__init__()
我想知道,在每个My type is <class '__main__.Base'>
My type is <class '__main__.Derived'>
内,定义方法的类,而不是派生类。所以我会得到以下印刷品:
+- [2] fork https://github.com/Microsoft/opencv
+- [3] branch https://github.com/Microsoft/opencv/tree/vs2015-samples
答案 0 :(得分:1)
使用super().__thisclass__
:
class Base(object):
def __init__(self):
print("My type is", super().__thisclass__)
class Derived(Base):
def __init__(self):
super().__init__()
print("My type is", super().__thisclass__)
d = Derived()
My type is <class '__main__.Base'>
My type is <class '__main__.Derived'>
这个课程不那么优雅但硬连线:
class Base(object):
def __init__(self):
print("My type is", Base)
class Derived(Base):
def __init__(self):
super().__init__()
print("My type is", Derived)
d = Derived()
输出:
My type is <class '__main__.Base'>
My type is <class '__main__.Derived'>