Does __init__ of subclass need the same argument types as __init__ of base class?

Sibylle Koczian nulla.epistola at web.de
Wed Mar 25 08:09:26 EDT 2009


I do more or less understand this error message:

>>> import datetime
>>> x1 = datetime.date.today()
>>> x2 = datetime.date(x1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: an integer is required

I don't understand at all why I get the same message with this little
script:

############################
import datetime

class meindatum(datetime.date):
    def __init__(self, datum):
        print "meindatum"
        datetime.date.__init__(self, datum.year, datum.month, datum.day)
        # Using super() doesn't make a difference:
         # super(meindatum, self).__init__(self, datum.year, datum.month,
        #                                datum.day)

x1 = datetime.date.today()
print repr(x1)
x2 = meindatum(x1)
print repr(x2)
#######################################

Executing this from the command line:

sib at Elend:~> python /windows/E/LinWin/Python/datum_ableiten.py
datetime.date(2009, 3, 25)
Traceback (most recent call last):
  File "/windows/E/LinWin/Python/datum_ableiten.py", line 12, in <module>
    x2 = meindatum(x1)
TypeError: an integer is required
sib at Elend:~>

The print command inside the __init__ method isn't executed, so that
method doesn't seem to start at all. Looks to me as if
meindatum.__init__() needs the same arguments as
datetime.date.__init__() does, but I can't really believe that (Python
isn't Pascal).

Using Python 2.6.1, tried this on Linux and Windows XP.

Thank you for explanations,
Sibylle




More information about the Python-list mailing list