2015-02-07 18 views
0

我想繼承datetime.date到一個新的對象,它需要一個額外的參數:當我嘗試做的一個實例超().__ new__電話:對象沒有參數

class FullDate: 
    def __new__(cls, lst, date): # initiate the date class - bit complicated 
     inst = super(FullDate, cls).__new__(cls, date.year, date.month, date.day) 
     # do stuff 

迄今爲止,我得到下面的錯誤:

Traceback (most recent call last): 
    File "<pyshell#55>", line 8, in <module> 
    to_load = FullDate(y[key], key) 
    File "/home/milo/Documents/Codes/PyFi/lib/Statement/Classes.py", line 518, in __new__ 
    inst = super(FullDate, cls).__new__(cls, date.year, date.month, date.day) 
TypeError: object() takes no parameters 

我一直在研究爲什麼發生這種情況,但都拿出了空爲止。

+0

既然你[以前繼承'datetime.date'(http://stackoverflow.com/q/283​​32396/3001761),你爲什麼要刪除它? – jonrsharpe 2015-02-07 17:05:55

+0

@jonrsharpe這讓我感到最驚訝!我認爲我在更新類時繼承了它,然後將錯誤的文件推送到了git。現在感覺啞巴... – Scironic 2015-02-07 17:06:53

回答

2

您實際上並不從datetime.date導出FullDate

嘗試

import datetime 
class FullDate(datetime.date): 
... 

不過,我不能肯定這是去上班了像你希望它會; datetime.date實際上來自C庫,在大多數實現中。

1

您未擴展datetime.date。嘗試:

class FullDate(date): 

如果省略基類(日期),你實際上是擴展其在它的構造函數沒有參數的object

1

兩件事。首先,確保你實際上是從日期繼承的。其次,更常見的模式是在子類上定義新的__init__方法。喜歡的東西:

def __init__(self, new_arg, *args, **kwargs): 
    self.new_arg = new_arg 
    super(child_class, self).__init__(args, kwargs) 
+0

我同意,但必須引用你對此前的q:http://stackoverflow.com/questions/28332396/init-argument-mismatch-between-super-and-subclass在這種特殊情況下__init__ doesn沒有工作。 – Scironic 2015-02-07 17:15:46

相關問題