blackforestcowboy
blackforestcowboy

Reputation: 1021

Method assignment and objects

i've got a problem with python: I want to assign a method to an object form another class, but in this method use its own attributes. Since i have many container with different use methods in my project (not in that example) i dont want to use inheritance, thad would force me to create a custom class for each instance.

class container():
    def __init__(self):
        self.info = "undefiend info attribute"

    def use(self):
        print self.info


class tree():
    def __init__(self):

        # create container instance
        b = container()

        # change b's info attribute
        b.info = "b's info attribute"

        # bound method test is set as use of b and in this case unbound, i think
        b.use = self.test

        # should read b's info attribute and print it
        # should output: test: b's info attribute but test is bound in some way to the tree object
        print b.use()

    # bound method test
    def test(self):
        return "test: "+self.info


if __name__ == "__main__":
    b = tree()

Thank you very much for reading this, and perhaps helping me! :)

Upvotes: 3

Views: 1551

Answers (4)

S.Lott
S.Lott

Reputation: 391854

Do not move methods around dynamically.

Just Use Delegation. Avoid Magic.

Pass the "Tree" object to the Container. It saves trying to move methods around.

class Container( object ):
    def use( self, context ):
        print context.info
        context.test()

class Tree( object ):
    def __init__( self, theContainerToUse ):
        b= theContinerToUse( self )
        print b.use()
    def test( self ):
        print "test"+self.info

Upvotes: 1

Laurence Gonsalves
Laurence Gonsalves

Reputation: 143154

Use tree.test instead of self.test. The method attributes of an instance are bound to that instance.

Upvotes: 1

Unknown
Unknown

Reputation: 46781

Here you go. You should know that self.test is already bound since by the time you are in __init__ the instance has already been created and its methods are bound. Therefore you must access the unbound member by using the im_func member, and binding it with MethodType.

import types

class container():
    def __init__(self):
        self.info = "undefiend info attribute"

    def use(self):
        print self.info


class tree():
    def __init__(self):

        # create container instance
        b = container()

        # change b's info attribute
        b.info = "b's info attribute"

        # bound method test is set as use of b and in this case unbound, i think
        b.use = types.MethodType(self.test.im_func, b, b.__class__)

        # should read b's info attribute and print it
        # should output: test: b's info attribute but test is bound in some way to the tree object
        print b.use()

    # bound method test
    def test(self):
        return "test: "+self.info


if __name__ == "__main__":
    b = tree()

Upvotes: 2

DevDevDev
DevDevDev

Reputation: 5177

Looks like you are trying to use inheritance? The tree inherits from the container?

Upvotes: 1

Related Questions