Python: 你能从包含函数的变量中找到父 class 吗?

Python: Can you find the parent class from a variable containing a function?

给定以下程序:

from functools import update_wrapper                                                                                      

class MyClass:                                                                                                            
    @classmethod                                                                                                          
    def my_function(cls):                                                                                                 
        def another_function():                                                                                           
            print('hello')                                                                                                
        return update_wrapper(another_function, cls)                                                                      

def do_something(the_func):                                                                                              
    print(the_func)                                                                                                             
    # <function MyClass at 0x7f5cb69fd848>                                                                               
    print(the_func.__class__)                                                                                                   
    # <type 'function'>                                                                                                  
    print(the_func())                                                                                                     

x = MyClass()                                                                                                            
y = x.my_function()                                                                                                      

do_something(y)                                                                                                          

在我的 do_something 函数中,如何识别 'the_func' 变量来自 'MyClass' class?具体来说,如何获得对 MyClass 的未实例化引用?

print(dir(the_func))

...returns 没什么明显的。

看看__wrapped__ dunder:

>>> y.__wrapped__
__main__.MyClass

functools.update_wrapper加了这个属性。

我还想指出您对 update_wrapper 的用法有些奇怪。在这里使用 my_functionanother_functionanother_functioncls 更常见。然后,您将通过 __wrapped____self__ 访问 class 对象。

当您打印 the_func 时,您将获得函数对象。 所以 the_func.__name__ 会给你那个 class 函数的名字!

from functools import update_wrapper                                                                                      

class MyClass:                                                                                                            
    @classmethod                                                                                                          
    def my_function(cls):                                                                                                 
        def another_function():                                                                                           
            print('hello')                                                                                                
        return update_wrapper(another_function, cls)                                                                      

def do_something(the_func):                                                                                              
    print(the_func)                                                                                                             
    # <function MyClass at 0x7f5cb69fd848>                                                                               
    print(the_func.__class__)                                                                                                   
    # <type 'function'>                                                                                                  
    print(the_func.__name__)                                                                                                     
    #MyClass

x = MyClass()                                                                                                            
y = x.my_function()                                                                                                      

do_something(y)