I want to add one element to list in one variable with Data class. Why when I append condition class to a data list class it is added to all other variables as well? What I'm doing wrong here? Whole code below:
from copy import copy
class Condition:
def __init__(self, name, time=2):
self.name = name
self.time = time
class Data:
def __init__(self, name, state=[]):
self.name = name
self.state = state
pc = Data("pc1")
pc2 = Data("pc2")
dps_buff = Condition('data')
pc.state.append(copy(dps_buff))
counter = 1
while counter < 10:
for condition in pc.state:
if condition.time > 0:
condition.time -= 1
print pc.state, 1
print pc2.state, 2
elif condition.time == 0:
print 'empty', condition.time
pc.state.remove(condition)
counter += 1
print pc.state
print pc2.state
Output:
C:\Python27\python.exe "D:/Python/Tests/test.py"
[<__main__.Condition instance at 0x0000000002CAF408>] 1
[<__main__.Condition instance at 0x0000000002CAF408>] 2
[<__main__.Condition instance at 0x0000000002CAF408>] 1
[<__main__.Condition instance at 0x0000000002CAF408>] 2
empty 0
[]
[]
Intended output:
C:\Python27\python.exe "D:/Python/Tests/test.py"
[<__main__.Condition instance at 0x0000000002CAF408>] 1
[] 2
[<__main__.Condition instance at 0x0000000002CAF408>] 1
[] 2
empty 0
[]
[]