在Python中创建多个类或多个对象?

时间:2016-02-05 14:30:28

标签: python

我有以下问题,我需要有关如何在Python中以最佳技术方式解决它的建议。由于我是编程新手,我想提出一些建议。

所以我将拥有以下对象,他们应该存储一些东西。这是一个例子:

  1. 对象1:现金股息(它们将具有以下属性)

    • exdate(将存储日期列表)
    • recorddate(将存储日期列表)
    • paydate(将存储日期列表)
    • ISIN(将存储文本列表)
  2. 对象2:stocksplits(他们将拥有以下prpoerties)

    • stockplitratio(将是一些口粮)
    • exdate(日期列表)
    • ...
  3. 我试图像这样解决它:

    class cashDividends(object):
    
        def __init__(self, _gross,_net,_ISIN, _paydate, _exdate, _recorddate, _frequency, _type, _announceddate, _currency):
            self.gross = _gross
            self.net = _net
            self.ISIN = _ISIN
            self.paydate = _paydate
            self.exdate = _exdate
            self.recorddate = _recorddate
            self.frequency = _frequency
            self.type = _type
            self.announceddate = _announceddate
            self.currency = _currency
    

    所以,如果我有这个,我将不得不创建另一个名为stockplits的类,然后再次定义__init__函数。

    然而,有没有办法可以让我有一个类如“公司行为”,然后在那里进行股票拆分和cashdividends

3 个答案:

答案 0 :(得分:1)

如果我理解正确你想要的是一个对象,它有你创建的属性中的其他对象?

class CorporateActions(object):
    def __init__(self, aCashDividend, aStockSplit):
        self.cashDividend = aCashDividend
        self.stockSplit = aStockSplit


myCashDividends = CashDividends(...) #corresponding parameters here
myStockSplit = StockSplit(...)
myCorporateActions = CorporateActions(myCashDividends, myStockSplit)

答案 1 :(得分:1)

当然可以!在python中,您可以将类传递给其他类。 这是一个简单的例子:

mySQL_Job();

function mySQL_Job(some_values){
   tx.executeSql("SELECT * FROM myTable", [], function(tx, res) {

      //Here goes your result handling, like inserting values in your html

}, function(error) {
    console.log('SQLite error: ' + error.message);
  });
}

答案 2 :(得分:0)

严格地说,这个答案不是最后一个问题的答案。但是,这是一种让您的生活更轻松的方式。

考虑创建一个排序模板类(我在松散地使用这个术语;在Python中没有这样的东西)__init__为你工作。像这样:

class KwargAttrs():
    def __init__(self, **kwargs):
        for k,v in kwargs.items():
            setattr(self, k, v)
    def _update(self, **kwargs):
        args_dict = {k:(kwargs[k] if k in kwargs else self.__dict__[k]) for k in self.__dict__} 
        self.__dict__.update(args_dict)

此类使用每个提供的关键字参数作为对象属性。以这种方式使用它:

class CashDividends(KwargAttrs):
    def __init__(self, gross, net, ISIN, paydate, exdate, recorddate, frequency, type, announceddate, currency):
        # save the namespace before it gets polluted
        super().__init__(**locals()) 

        # work that might pollute local namespace goes here

        # OPTIONAL: update the argument values in case they were modified: 
        super()._update(**locals())

使用这样的方法,您不必遍历参数列表并分配每个对象属性;它会自动发生。

我们通过__init__对父类进行方法调用,在super()方法中完成您需要完成的所有操作。我们这样做是因为locals()在函数的当前命名空间中返回dict每个变量,因此您需要1.)在任何其他工作污染它之前捕获该命名空间,并且2.)更新命名空间以防任何工作会更改参数值。

update的调用是可选的,但是如果在调用super().__init__()之后对它们执行了某些操作,则不会更新提供的参数的值(即,除非您使用更改值setattr(self, 'argname,值)`,这不是一个坏主意。

您可以继续使用此类:

class StockSplits(KwargAttrs):
    def __init__(self, stocksplitratio, gross, net, ISIN, paydate, exdate, recorddate, frequency, type, announceddate, currency):
        super().__init__(**locals())

如其他答案中所述,您可以为我们的其他类创建容器,但您甚至可以使用相同的模板类来创建容器:

class CorporateActions(KwargAttrs):
    def __init__(self, stock_splits , cash_dividends):
        super().__init__(**locals())

ca = CorporateActions(stock_splits = StockSplits(<arguments>), cash_dividends = CashDividends(<arguments>) )