[英]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? 在Python 3中,有没有一种方法可以存储一个类的实例以供另一个类的方法多次使用? 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. 我的问题来自ObjectTwo中的change_object方法。 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. 可能有更好的方法来执行此操作,但是从本质上讲,我希望能够保存ObjectOne的更新后的新实例,以便下次我调用change_object时可以引用该新实例,并更改该实例而不是基础实例。初始化的ObjectOne实例。
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. 在当前情况下,每当我尝试调用change_object时,它都会在新的ObjectOne实例中更改ObjectTwo的初始化,但是只是更新ObjectOne的默认初始化,因为change_object不知道我上次调用它时更新的ObjectOne实例。 (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) (编辑:更改了ObjectOne的__init__
以使我的问题更清楚)
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__
: 假设我理解正确的你-你想创建修改后的单独实例ObjectOne
每一个实例ObjectTwo
然后用change_object
上的相应实例ObjectOne
,我建议建立的修改实例ObjectOne
内__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
; 我还建议考虑定义一个新类,例如ObjectOneModified
,该类继承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
. 此类将在ObjectTwo
而不是ObjectOne
,并且可以修改其__init__
而不影响ObjectOne
的原始(默认)定义。
I ended up solving my problem by passing ObjectOne
to ObjectTwo
through ObjectTwo
's __init__
, like so: 我最终通过将解决我的问题ObjectOne
到ObjectTwo
通过ObjectTwo
的__init__
,就像这样:
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. 这类似于ViAik的建议,但不涉及创建新类或初始化全新实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.