Python 从另一个类更改实例变量
我试图从另一个类更改下面代码中的Python 从另一个类更改实例变量,python,class,Python,Class,我试图从另一个类更改下面代码中的self.var\u 1,但我收到错误test1没有属性“var\u 1”。我觉得我犯了一个简单的错误,但似乎找不到问题所在 class test1: def __init__(self): self.var_1 = "bob" instance2 = test2() def change_var(self, new_var): print(self.var_1) #should print "bo
self.var\u 1
,但我收到错误test1没有属性“var\u 1”
。我觉得我犯了一个简单的错误,但似乎找不到问题所在
class test1:
def __init__(self):
self.var_1 = "bob"
instance2 = test2()
def change_var(self, new_var):
print(self.var_1) #should print "bob"
self.var_1 = new_var #should change "bob" to "john"
print(self.var_1) #should print "john"
class test2:
def __init__(self):
test1.change_var(test1, "john")
instance = test1()
var_1
是一个实例变量,因此需要使用该实例:
class test1:
def __init__(self):
self.var_1 = "bob"
instance2 = test2(self) # <<<<<< pass test1 instance
def change_var(self, new_var):
print(self.var_1) #should print "bob"
self.var_1 = new_var #should change "bob" to "john"
print(self.var_1) #should print "john"
class test2:
def __init__(self, t1obj): # <<<< take test1 instance
t1obj.change_var("john") # <<<< use test1 instance
instance = test1()
var_1
是一个实例变量,因此需要使用该实例:
class test1:
def __init__(self):
self.var_1 = "bob"
instance2 = test2(self) # <<<<<< pass test1 instance
def change_var(self, new_var):
print(self.var_1) #should print "bob"
self.var_1 = new_var #should change "bob" to "john"
print(self.var_1) #should print "john"
class test2:
def __init__(self, t1obj): # <<<< take test1 instance
t1obj.change_var("john") # <<<< use test1 instance
instance = test1()
根据你的评论,我觉得你想为你的班级创造一个新的机会
def singleton(class_):
instances = {}
def getinstance(*args, **kwargs):
if class_ not in instances:
instances[class_] = class_(*args, **kwargs)
return instances[class_]
return getinstance
@singleton
class Test1(object):
def __init__(self):
self.var_1 = "bob"
def change_var(self, new_var):
print(self.var_1) #should print "bob"
self.var_1 = new_var #should change "bob" to "john"
print(self.var_1) #should print "john"
class Test2(object):
def __init__(self):
test1 = Test1()
test1.change_var("john")
Test2()
有多种方法可以实现这一点,其中大部分可以在中找到
我创建了一个使用上面答案中概述的第一个方法的装饰器,并将其应用到您的类中
def singleton(class_):
instances = {}
def getinstance(*args, **kwargs):
if class_ not in instances:
instances[class_] = class_(*args, **kwargs)
return instances[class_]
return getinstance
@singleton
class Test1(object):
def __init__(self):
self.var_1 = "bob"
def change_var(self, new_var):
print(self.var_1) #should print "bob"
self.var_1 = new_var #should change "bob" to "john"
print(self.var_1) #should print "john"
class Test2(object):
def __init__(self):
test1 = Test1()
test1.change_var("john")
Test2()
您可以在上查看此内容,并查看与输出匹配的结果
鲍勃
约翰
根据你的评论,我觉得你想为你的班级创造一个新的机会
def singleton(class_):
instances = {}
def getinstance(*args, **kwargs):
if class_ not in instances:
instances[class_] = class_(*args, **kwargs)
return instances[class_]
return getinstance
@singleton
class Test1(object):
def __init__(self):
self.var_1 = "bob"
def change_var(self, new_var):
print(self.var_1) #should print "bob"
self.var_1 = new_var #should change "bob" to "john"
print(self.var_1) #should print "john"
class Test2(object):
def __init__(self):
test1 = Test1()
test1.change_var("john")
Test2()
有多种方法可以实现这一点,其中大部分可以在中找到
我创建了一个使用上面答案中概述的第一个方法的装饰器,并将其应用到您的类中
def singleton(class_):
instances = {}
def getinstance(*args, **kwargs):
if class_ not in instances:
instances[class_] = class_(*args, **kwargs)
return instances[class_]
return getinstance
@singleton
class Test1(object):
def __init__(self):
self.var_1 = "bob"
def change_var(self, new_var):
print(self.var_1) #should print "bob"
self.var_1 = new_var #should change "bob" to "john"
print(self.var_1) #should print "john"
class Test2(object):
def __init__(self):
test1 = Test1()
test1.change_var("john")
Test2()
您可以在上查看此内容,并查看与输出匹配的结果
鲍勃
约翰
@SunnyPatel不起作用,产生错误
change\u var(),缺少1个必需的位置参数:“new\u var”
No。这将不正确。您可以在test2
内部实例化test1
并调用实例。在那里更改\u var
。@SunnyPatel这听起来像是一个混乱的解决方案,我在GUI中使用它,两次实例化test1会打开一堆随机帧。如果您只想有一个实例,可能应该实现一个。@SunnyPatel,不起作用,产生错误change\u var(),缺少1个必需的位置参数:“new\u var”
No。这将不正确。您可以在test2
中实例化test1
并调用实例。在那里更改\u var
。@SunnyPatel这听起来像是一个混乱的解决方案,我在GUI中使用它,两次实例化test1会打开一堆随机帧。如果您只想拥有一个实例,可能应该实现一个。