简体   繁体   中英

Store an instance of a class within another class method

Is there a way in Python 3 to be able to store an instance of a class for use in another class's method multiple times? I have classes like so...

class ObjectOne:

    def __init__(self):

        #initializes multiple different instances of ObjectTwo
        self.ObjectTwoInstance = ObjectTwo(...)

    def print_output(self):

        #prints some output from initialized objects

class ObjectTwo:

    def __init__(self):

        #some initialization

    def change_object(self):
        """Changes some state of an ObjectTwo Instance"""

        #creates a new instance of ObjectOne, and overrides
        #the default ObjectOne instance's initialization of
        #ObjectTwo. (I want to be able to refer back to this
        #new instance of ObjectOne, and change that instead
        #of changing another default ObjectOne object)

My issue comes with the method change_object within ObjectTwo. There is probably a better way to do this, but essentially I want to be able to save the updated, new instance of ObjectOne so that the next time I call change_object I can refer to that new instance, and change that one instead of the base initialized ObjectOne instance.

In the current situation, whenever I try to call change_object, it will change ObjectTwo's init within the new ObjectOne instance, but just updates the default initialization of ObjectOne because change_object has no knowledge of the newer updated instance of ObjectOne the last time I called it. (I'm trying not to be confusing, but this is the best way I've been able to explain it)

If there is an easier way to do this, or do I need to restructure my code? In either case, I'm not sure how I would go about this.

(Edited: Changed the __init__ of ObjectOne to make my question more clear)

I'm not 100% sure on what you're trying to do, but it appears that there's definitely a better way to do this...

Assuming that I understood you correctly - you're trying to create separate instances of the modified ObjectOne per an instance of ObjectTwo and then use change_object on the corresponding instance of ObjectOne , I would suggest creating the modified instance of ObjectOne within __init__ :

class ObjectTwo:
    def __init__(self):
        self.ObjectOneInstance = ObjectOne()
        # override the default ObjectOne's initialization of ObjectTwo,
        # however you did it originally in change_object

        # some other initialization

    def change_object(self):
        # change some state of the ObjectTwo instance
        # perform operation on self.ObjectOneInstance

I would also suggest considering defining a new class, such as ObjectOneModified , which inherits ObjectOne ; this class would be used in ObjectTwo instead of ObjectOne , and you can modify its __init__ without affecting the original (default) definition of ObjectOne .

I ended up solving my problem by passing ObjectOne to ObjectTwo through ObjectTwo 's __init__ , like so:

class ObjectTwo:

    def __init__(self, object_one):
        self.object_one_instance = object_one

    def change_object(self):
        """Changes some state of an ObjectTwo Instance"""

        #modifies self.object_one_instance, and updates
        #the instance

This is similar to what ViAik suggested, but doesn't involve creating a new class, or initializing a brand new instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM