2016-02-05 160 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>))