Apply a method to an object of another class

后端 未结 3 818
感情败类
感情败类 2020-12-19 10:48

Given two non-related classes A and B, how to call A.method with an object of B as self?

class A:
    def __init__(self, x):
               


        
3条回答
  •  清酒与你
    2020-12-19 11:12

    In Python 3.x you can simply do what you want:

    A.print_x(b) #<-- 'eggs'
    

    If you only have an instance of 'A', then get the class first:

    a.__class__.print_x(b) #<-- 'eggs'
    

    In Python 2.x (which the OP uses) this doesn't work, as noted by the OP and explained by Amber in the comments:

    This is a difference between Python 2.x and Python 3.x - methods in 3.x don't enforce being passed the same class.

    More details (OP edit)

    In python 2, A.print_x returns an "unbound method", which cannot be directly applied to other classes' objects:

    When an unbound user-defined method object is called, the underlying function (im_func) is called, with the restriction that the first argument must be an instance of the proper class (im_class) or of a derived class thereof. >> http://docs.python.org/reference/datamodel.html

    To work around this restriction, we first have to obtain a "raw" function from a method, via im_func or __func__ (2.6+), which then can be called passing an object. This works on both classes and instances:

    # python 2.5-
    A.print_x.im_func(b)    
    a.print_x.im_func(b)
    
    # python 2.6+
    A.print_x.__func__(b)   
    a.print_x.__func__(b)
    

    In python 3 there's no such thing anymore as unbound method.

    Unbound methods are gone for good. ClassObject.method returns an ordinary function object, instance.method still returns a bound method object. >> http://www.python.org/getit/releases/3.0/NEWS.txt

    Hence, in python 3, A.print_x is just a function, and can be called right away and a.print_x still has to be unbounded:

    # python 3.0+
    A.print_x(b)
    a.print_x.__func__(b)
    

提交回复
热议问题