Subclassing standard class: how to write my own __init__?

kpalamartchouk at kpalamartchouk at
Fri Dec 19 08:21:06 EST 2008

Dear All,

I am trying to create a class that would extend functionality of by implementing some functions I need, for example an
optional initialisation by (year, day_of_year) instead of (year,
month, day). I would like the class constructor to behave in the
datetime's default way if there are no keyword arguments and do my own
stuff if there are some.

Here is the minimal example:

from datetime import date, timedelta

class MyDateTime(date):
    def __init__(self, *arg, **kw):
        if len(kw):
            year = kw['year']
            doy = kw['doy']
            my_date = date(year=year, month=1, day=1) + timedelta
            date.__init__(self, year = my_date.year, month =
my_date.month, day =
            date.__init__(self, *arg)

date1 = MyDateTime(2008, 12, 19)
date2 = MyDateTime(year=2008, doy=354)

It works fine when I don't supply any keyword arguments. But if I do,
TypeError is happening:

TypeError: function takes exactly 3 arguments (1 given)

Could you help me to understand where I am wrong?


More information about the Python-list mailing list