2016-02-05 158 views
0

我有以下问题,我需要关于如何在Python中以最好的技术解决它的建议。由于我是编程新手,我想提供一些建议。在Python中创建多个类或多个对象?

所以我会有以下的对象,他们应该存储的东西。这里有一个例子:

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

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

    • stockplitratio(会有一些比)
    • exdate(日期列表)
    • ...

我试图这样解决它:

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在那里?

+1

我认为python支持内部类(类内的类) –

+0

正如你所说,你可以创建一个具有'__init__'的类,它只设置所需的参数。然后定义其他方法来预制或存储其他任务。看看第一个数字[here](http://www.jesshamrick.com/2011/05/18/an-introduction-to-classes-and-inheritance-in-python/)。最终,您可能希望创建一个定义常用方法的主类和执行特定工作的子类。该概念被称为*继承*,该链接也被描述。 – Oniow

回答

1

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

class A(): 
    def __init__(self): 
     self.x = 0 


class B(): 
    def __init__(self): 
     self.x = 1 


class Container(): 
    def __init__(self,objects): 
     self.x = [obj.x for obj in objects] 

a = A() 
b = B() 
c = Container([a,b]) 

c.x 

[0,1] 
+0

什么其实我是想在我的main.py然后调用如下: mycashdiv = cashdiv() mycashdiv.exdate = [BLA,BAL] mycashdiv.recorddate = [喇嘛,喇嘛] mystocksplit = stockplit( ) mystocksplit.newshares = [3,2,3] ... ... >> – Nant

+0

@Nant我没有得到你最后的评论..你能解释一下吗? –

+0

所以基本上我希望用户在任何Python文件叫什么如下: cashdividend =(输入您的现金股利参数) stocksplits =(输入您的股票分割参数) 像我们正常发起整数或字符串。我只想拥有自己的对象,如现金分红,问题是我如何为此创建一个课程 – Nant

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) 
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()) 

使用这样的方法,您不必通过参数列表并指定每个对象属性;它会自动发生。

我们通过super()通过方法调用父级的方法来取代您在__init__方法中完成的所有工作。我们这样做是因为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>))