Adding method from one class to another class or to instance ofanother class

Discussion in 'Python' started by marekw2143, Jul 24, 2009.

  1. marekw2143

    marekw2143 Guest

    Hi,

    I have one class (A) that has defined method createVars. I would like
    to add that method to class B
    The code looks like this:


    class A(object):
    def createVars(self):
    self.v1 = 1
    self.v2 = 3
    pass

    class B(object):
    pass


    I don't want to use inheritance (because class A has many methods
    defined that class B doesn't need).
    When I try the folloowing:


    B.createVars = C.createVars
    B().createVars()


    then the following error occurs:
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: unbound method createVars() must be called with A instance
    as first argument (got nothing instead)

    When I try to add the createVars method to instance of B:

    >>> b=B()
    >>> b.createVars = new.instancemethod(A.createVars, b, B)
    >>> b.createVars

    <bound method B.createVars of <__main__.B object at 0x7f6330cc4a90>>
    >>> b.createVars()




    Then the following error raises:


    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: unbound method createVars() must be called with A instance
    as first argument (got B instance instead)



    How can I solve this problem?

    Regards,
    Marek
    marekw2143, Jul 24, 2009
    #1
    1. Advertising

  2. marekw2143

    Peter Otten Guest

    Re: Adding method from one class to another class or to instance of another class

    marekw2143 wrote:

    > Hi,
    >
    > I have one class (A) that has defined method createVars. I would like
    > to add that method to class B
    > The code looks like this:
    >
    >
    > class A(object):
    > def createVars(self):
    > self.v1 = 1
    > self.v2 = 3
    > pass
    >
    > class B(object):
    > pass
    >
    >
    > I don't want to use inheritance (because class A has many methods
    > defined that class B doesn't need).


    You can move createVars() into a mixin or common base class:

    class M(object):
    def createVars(self): ...

    class A(M):
    ...

    class B(M)
    ...

    > When I try the folloowing:
    >
    >
    > B.createVars = C.createVars
    > B().createVars()
    >
    >
    > then the following error occurs:
    > Traceback (most recent call last):
    > File "<stdin>", line 1, in <module>
    > TypeError: unbound method createVars() must be called with A instance
    > as first argument (got nothing instead)
    >
    > When I try to add the createVars method to instance of B:
    >
    >>>> b=B()
    >>>> b.createVars = new.instancemethod(A.createVars, b, B)
    >>>> b.createVars

    > <bound method B.createVars of <__main__.B object at 0x7f6330cc4a90>>
    >>>> b.createVars()

    >
    >
    >
    > Then the following error raises:
    >
    >
    > Traceback (most recent call last):
    > File "<stdin>", line 1, in <module>
    > TypeError: unbound method createVars() must be called with A instance
    > as first argument (got B instance instead)
    >
    >
    >
    > How can I solve this problem?


    >>> class A(object):

    .... def create_vars(self):
    .... self.x = 42
    ....
    >>> class B(object): pass

    ....
    >>> B.create_vars = A.create_vars.im_func
    >>> b = B()
    >>> b.create_vars()
    >>> b.x

    42

    An alternative I find a bit cleaner:

    >>> def create_vars(self): self.x = 42

    ....
    >>> class A(object):

    .... create_vars = create_vars
    ....
    >>> class B(object):

    .... create_vars = create_vars
    ....
    >>> b = B()
    >>> b.create_vars()
    >>> b.x

    42

    Peter
    Peter Otten, Jul 24, 2009
    #2
    1. Advertising

  3. marekw2143

    Terry Reedy Guest

    Re: Adding method from one class to another class or to instanceof another class

    marekw2143 wrote:
    > Hi,
    >
    > I have one class (A) that has defined method createVars. I would like
    > to add that method to class B
    > The code looks like this:
    >
    >
    > class A(object):
    > def createVars(self):
    > self.v1 = 1
    > self.v2 = 3
    > pass
    >
    > class B(object):
    > pass
    >
    >
    > I don't want to use inheritance (because class A has many methods
    > defined that class B doesn't need).
    > When I try the folloowing:
    >
    >
    > B.createVars = C.createVars


    you meant A.createVars

    > B().createVars()
    >
    >
    > then the following error occurs:
    > Traceback (most recent call last):
    > File "<stdin>", line 1, in <module>
    > TypeError: unbound method createVars() must be called with A instance
    > as first argument (got nothing instead)


    In 3.1, your example works fine. The difference is that in 2.x,
    B.createVars is a method wrapperthat wraps the function, whereas in 3.1,
    it is the function itself. For 2.x, you need to extract the function
    from the wrapper. It is im_func or something like that. Use
    dir(B.createVars) to check for sure.

    > How can I solve this problem?


    Terry Jan Reedy
    Terry Reedy, Jul 24, 2009
    #3
  4. marekw2143

    marekw2143 Guest

    Thanks for your responses. im_func is all I need. I considered
    subclassing, wchih is more easy to extend, but I needed some quick way
    to add a method to another class.

    Regards,
    Marek
    marekw2143, Jul 25, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page