0

I'm trying to inherit a datetime.date into a new object which takes an extra parameter:

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

When I try to make an instance of the date I get the below error:

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

I've been researching why this happens but have come up empty so far.

NDevox
  • 4,056
  • 4
  • 21
  • 36
  • Given that you were [previously inheriting from `datetime.date`](http://stackoverflow.com/q/28332396/3001761), why did you remove it?! – jonrsharpe Feb 07 '15 at 17:05
  • @jonrsharpe That's what surprised me the most! I reckon I inherited while I was updating the class, then pushed the wrong file to git. Feel dumb now... – NDevox Feb 07 '15 at 17:06

3 Answers3

2

You don't actually derive FullDate from datetime.date.

try

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

However, I'm not quite sure this is going to work out like you hope it will; datetime.date actually comes from a C library, in most implementations.

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
1

You are not extending datetime.date. Try:

class FullDate(date):

If you omit the base class (date), you are actually extending the object which has no parameters in its constructor.

Selcuk
  • 57,004
  • 12
  • 102
  • 110
1

Two things. First, make sure you are actually inheriting from date. Second, the more common pattern is to define a new __init__ method on the child class. Something like:

def __init__(self, new_arg, *args, **kwargs):
    self.new_arg = new_arg
    super(child_class, self).__init__(args, kwargs)
Sean Azlin
  • 886
  • 7
  • 21
  • I agree, but must refer you to this previous q: http://stackoverflow.com/questions/28332396/init-argument-mismatch-between-super-and-subclass In this particular case __init__ doesn't work. – NDevox Feb 07 '15 at 17:15