初学请教:如何传递值给class
http://www.itmaybeahack.com/book/python-2.6/html/p03/p03c01_class.html#stock-valuation
想做下上面的股票例子,但是遇到问题了,
#!/usr/bin/env python
class StockBlock(object):
""" A stock block class which has the purchase date,
price per share and number of shares. """
def __int__(self,date,price,number):
''' populate the individual fields of date,price,
and number of shares.'''
self.date = date
self.price = price
self.number = number
def getPurchValue(self):
'''compute the value as purchase price per share X shares.'''
purchvalue = self.price * self.number
return purchvalue
def getSaleValue(self):
'''compute the value as price per share X shares.'''
salevalue = self.price * self.number
return salevalue
def getROI(self):
'''computes the return on investment as(sale value - purchase value)/purchase value.'''
roi = (salevalue - purchvalue) / purchvalue
return roi
def __str__(self):
'''return a nicely formatted string that shows the
date,price and shares.'''
return self.date,self.price,self.number
def main():
totalGM = 0
for s in blocksGM:
totalGM += s.getPurchValue()
print(totalGM)
blocksGM = [
StockBlock( purchDate='25-Jan-2001', purchPrice=44.89, shares=17 ),
StockBlock( purchDate='25-Apr-2001', purchPrice=46.12, shares=17 ),
StockBlock( purchDate='25-Jul-2001', purchPrice=52.79, shares=15 ),
StockBlock( purchDate='25-Oct-2001', purchPrice=37.73, shares=21 ),
]
blocksEK = [
StockBlock( purchDate='25-Jan-2001', purchPrice=35.86, shares=22 ),
StockBlock( purchDate='25-Apr-2001', purchPrice=37.66, shares=21 ),
StockBlock( purchDate='25-Jul-2001', purchPrice=38.57, shares=20 ),
StockBlock( purchDate='25-Oct-2001', purchPrice=27.61, shares=28 ),
]
#!/usr/bin/env python
class StockBlock(object):
""" A stock block class which has the purchase date,
price per share and number of shares. """
def __init__(self,date,price,number):
''' populate the individual fields of date,price,
and number of shares.'''
self.date = date
self.price = price
self.number = number
def getPurchValue(self):
'''compute the value as purchase price per share X shares.'''
return self.price * self.number
def getSaleValue(self):
'''compute the value as price per share X shares.'''
return self.price * self.number
def getROI(self):
'''computes the return on investment as(sale value - purchase value)/purchase value.'''
roi = (salevalue - purchvalue) / purchvalue
return roi
def __str__(self):
'''return a nicely formatted string that shows the
date,price and shares.'''
return "%s %s %s"%(self.date, self.price, self.number)
def getDate(self):
return self.date
def getPrice(self):
return self.price
def getNumber(self):
return self.number
blocksGM = [
StockBlock('25-Jan-2001', 44.89, 17),
StockBlock('25-Apr-2001', 46.12, 17),
StockBlock('25-Jul-2001', 52.79, 15),
StockBlock('25-Oct-2001', 37.73, 21),
]
blocksEK = [
StockBlock('25-Jan-2001', 35.86, 22),
StockBlock('25-Apr-2001', 37.66, 21),
StockBlock('25-Jul-2001', 38.57, 20),
StockBlock('25-Oct-2001', 27.61, 28),
]
if __name__ == '__main__':
totalGM = 0
for s in blocksGM:
totalGM += s.getPurchValue()
print "date: ", s.getDate()
print "price: ", s.getPrice()
print "number: ", s.getNumber()
print totalGM