【问题标题】:Python: Can you find the parent class from a variable containing a function?Python:你能从包含函数的变量中找到父类吗?
【发布时间】:2017-02-28 02:47:11
【问题描述】:

给定以下程序:

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”类?具体来说,如何获得对 MyClass 的未实例化引用?

print(dir(the_func))

...返回任何明显的东西。

【问题讨论】:

    标签: python introspection


    【解决方案1】:

    看看__wrapped__dunder:

    >>> y.__wrapped__
    __main__.MyClass
    

    添加此属性的是functools.update_wrapper

    我还想指出,您对update_wrapper 的使用有些奇怪。在这里使用my_functionanother_function 而不是another_functioncls 会更常见。然后您将通过__wrapped____self__ 访问类对象。

    【讨论】:

    【解决方案2】:

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

    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)    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多