面向对象的实例变量问题
Object-oriented instance variable issue
我有以下代码:
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
当我 运行 它时,我会假设调用 s1.price
你会收到一个包含股票价格 A
的数组,而 s2.price
会得到这个价格库存 B
。但是当我 运行 它时, s1.price
和 s2.price
是相同的。
因此,当我将新值附加到 self.price
时,它似乎并没有将其附加到 class.
当前实例的变量中
谁能指出我做错了什么?
编辑:
当前输出:
[10 150] [10 150]
[10 150 10.2 150.3] [10 150 10.2 150.3]
期望的输出:
[10] [150]
[10 10.3] [ 150 150.3]
您正在将同一列表的引用传递给两个实例。列表是一个可变对象,所以它是 pass-by-reference.
一个解决方案是创建两个列表:
def test():
l_1 = []
l_2 = []
s1= Stock('A', l_1)
s2= Stock('B', l_2)
n=0
while n < 390:
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 #
但是,您还将在 class 外部附加 l_1 和 l_2,因为它们共享相同的引用。
由于 d1 和 d2 是价格列表,另一种解决方案是在实例化时创建一个列表,如果 Add_Price() 传递了一个列表,则扩展 Stock 的列表,如果不是列表,则附加一个价格。
股票class构造函数:
class Stock(object):
def __init__(self,name,prices=None):
self.name = name
self.price = prices or [] #create a new list on instantiation
def Add_Price(self,data):
if isinstance(data, list):
self.prices.extend(data)
else:
self.prices.append(data)
然后在你的 test() 函数中:
def test():
s1 = Stock('A')
s2 = Stock('B')
s1.Add_Price(d1[:390])
s2.Add_Price(d2[:390])
d1[:390]
是拼接,表示从index 0(含)到index 390(不含)的所有元素,这样就可以去掉while循环
我有以下代码:
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
当我 运行 它时,我会假设调用 s1.price
你会收到一个包含股票价格 A
的数组,而 s2.price
会得到这个价格库存 B
。但是当我 运行 它时, s1.price
和 s2.price
是相同的。
因此,当我将新值附加到 self.price
时,它似乎并没有将其附加到 class.
谁能指出我做错了什么?
编辑:
当前输出:
[10 150] [10 150]
[10 150 10.2 150.3] [10 150 10.2 150.3]
期望的输出:
[10] [150]
[10 10.3] [ 150 150.3]
您正在将同一列表的引用传递给两个实例。列表是一个可变对象,所以它是 pass-by-reference.
一个解决方案是创建两个列表:
def test():
l_1 = []
l_2 = []
s1= Stock('A', l_1)
s2= Stock('B', l_2)
n=0
while n < 390:
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 #
但是,您还将在 class 外部附加 l_1 和 l_2,因为它们共享相同的引用。 由于 d1 和 d2 是价格列表,另一种解决方案是在实例化时创建一个列表,如果 Add_Price() 传递了一个列表,则扩展 Stock 的列表,如果不是列表,则附加一个价格。
股票class构造函数:
class Stock(object):
def __init__(self,name,prices=None):
self.name = name
self.price = prices or [] #create a new list on instantiation
def Add_Price(self,data):
if isinstance(data, list):
self.prices.extend(data)
else:
self.prices.append(data)
然后在你的 test() 函数中:
def test():
s1 = Stock('A')
s2 = Stock('B')
s1.Add_Price(d1[:390])
s2.Add_Price(d2[:390])
d1[:390]
是拼接,表示从index 0(含)到index 390(不含)的所有元素,这样就可以去掉while循环