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

Python:您能从包含函数的变量中找到父类吗?,python,introspection,Python,Introspection,鉴于以下计划: from functools import update_wrapper class MyClass:

鉴于以下计划:

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)                                                                                                          

…未返回任何明显的内容。

查看
\uuuuuuuuuuuuuuuuu
dunder:

>>> y.__wrapped__
__main__.MyClass
正是它添加了这个属性


我还想指出,您对
update\u wrapper
的使用有些奇怪。在这里使用
my_函数
另一个_函数
比使用
另一个_函数
cls
更为常见。然后您可以通过
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
来访问类对象

当您打印函数时,您会得到函数对象。 因此,
函数名将为您提供函数所包装的类名的名称

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)    
从functools导入更新\u包装
类别MyClass:
@类方法
def my_功能(cls):
定义另一个函数()
打印('你好')
返回更新\u包装(另一个\u函数,cls)
定义做某事(函数):
打印(函数)
#                                                                                
打印(函数类)
#                                                                                                   
打印(函数名)
#我的班级
x=MyClass()
y=x.my_函数()
做点什么(y)

我想我的例子不好。我在这里有一个变量,其中包含作为_view()调用的结果,但它没有包装属性。不过,感谢您的指针,它确实适用于我的示例应用程序。
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)