首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

初学请问:怎么传递值给class

2013-09-05 
初学请教:如何传递值给classhttp://www.itmaybeahack.com/book/python-2.6/html/p03/p03c01_class.html#st

初学请教:如何传递值给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)



不知道如何提取下面这个代码中的价格,份数等值,class如何获得呀?

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


[解决办法]
引用:
感谢上面的回答,但是我还有问题:
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 ),
]


如果按照这个数据格式(有等号),应该如何提取数值呢?感觉应该有一种处理方法。
原文:We can load a simple database with a piece of code the looks like the following. The first statement will create a sequence with four blocks of stock. We chose variable name that would remind us that the ticker symbols for all four is ‘GM’.
就是根据等号前面的关键字提取,而不是使用匹配方式。

这里定义的StockBlock的实例,需要里面的参数与class定义的__init__中的参数名称一致

热点排行