[英]Given a classmethod reference, how to get a reference to the class in which it is defined?
我在代碼中的某個點有一個綁定的類方法引用。 我想從中獲取 class 本身。 我該怎么做?
class A:
@classmethod
def class_method(cls):
...
...
method_reference = A.class_method
...
reference_to_A = method_reference.bound_class # how to get it?
使用__self__
:
>>> class A:
... @classmethod
... def class_method(cls):
... pass
...
>>> method_reference = A.class_method
>>> method_reference.__self__
<class '__main__.A'>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.