我正在尝试拥有一个类(dprObject),它是多个 dprVariables 的容器,每个变量都有一个带有整数的列表(目前),但是当我将某些内容更改为一个变量时,它会更改为所有其他变量
在这种情况下,我得到的输出是
得分:5、6、3、4,位置:5、6、3、4,
但我想看看
得分:5、6,位置:3、4,
#!/usr/bin/python3
import pprint
def main():
d = dprObject() #new dpr object
score = dprVariable(d, "score") #new dpr variable
score.set(5) #set value
score.set(6)
position = dprVariable(d, "position")
position.set(3)
position.set(4)
print(d)
print("done")
class dprObject:
var = []
t = 0
def __init__(self):
pass
def __str__(self):
toReturn = ""
for v in self.var:
toReturn += v.toString()
return toReturn
def tick(self):
self.t += 1
def addVariable(self,v):
self.var.append(v)
class dprVariable:
name = ""
value = None
val = []
def __init__(self,d,name):
self.name = name
d.addVariable(self)
def __str__(self):
return self.toString()
def toString(self):
toReturn = self.name + ": "
for v in self.val:
toReturn += str(v) + ", "
return toReturn
def set (self, value):
self.value = value
self.val.append(value)
#class DprValue:
if __name__ == "__main__":
main()
任何帮助将不胜感激谢谢