I have the following code:
class Stock(object):
def __init__(self,name,price):
self.name = name
self.price = price
def Add_Price(self,data):
self.price.append(data)
def test():
l=[]
n=0
while n < 390:
s1= Stock('A', l)
s2= Stock('B', l)
s1.Add_Price(d1[n]) # d1 is a list with the prices for A #
s2.Add_Price(d2[n]) # d2 is a list with the prices for B #
print s1.price, s2.price
n=n+1
When I run it I would have assume that calling s1.price
you would receive an array with the price of stock A
and that s2.price
would have the price of stock B
. However when I run it, s1.price
and s2.price
are identical.
So it seems that when I append a new value to self.price
, it is not appending it to a variable of the current instance of the class.
Can anyone please point out what I'm doing wrong?
Edit:
Current output :
[10 150] [10 150]
[10 150 10.2 150.3] [10 150 10.2 150.3]
Desired output :
[10] [150]
[10 10.3] [ 150 150.3]