簡體   English   中英

給定一個類方法參考,如何獲得對其定義的 class 的參考?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM