【发布时间】:2016-09-14 10:10:08
【问题描述】:
我有两个班级:
class A:
def foo(self):
print(<get the defining class>)
class B(A):
pass
现在,我需要用这样的代码替换 <get the defining class>:
a = A()
b = B()
a.foo()
b.foo()
产生这个(即这是预期的行为):
A
A
我尝试了self.__class__.__name__,但这显然会为最后一次调用产生B,因为self 实际上属于B 类。
所以最终的问题是:如果我在一个方法体中(不是类方法),我怎样才能得到定义该方法的类的名称?
【问题讨论】:
-
我很困惑。如果 A & A 是错误的,并且 A & B 不是您所期望的 - 您实际期望的是什么?
-
@JonClements 不,A & A 是正确的。我会改写这个问题,感谢您指出困惑。
-
对...如果我们要做
class C: def foo(self) # whatever- 那么class D(B, C)foo“定义”在哪里?
标签: python class python-3.x methods