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

Discussion in 'Python' started by Sibylle Koczian, Mar 25, 2009.

  1. 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@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@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
     
    Sibylle Koczian, Mar 25, 2009
    #1
    1. Advertising

  2. Sibylle Koczian a écrit :
    (snip)
    >
    > 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)
    >
    > x1 = datetime.date.today()
    > print repr(x1)
    > x2 = meindatum(x1)
    > print repr(x2)
    > #######################################
    >
    > Executing this from the command line:
    >
    > sib@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@Elend:~>
    >
    > The print command inside the __init__ method isn't executed, so that
    > method doesn't seem to start at all.


    this often happens with (usually C-coded) immutable types. The
    initializer is not called, only the "proper" constructor (__new__). The
    following should work (not tested):

    class Meindatum(datetime.date):
    def __new__(self, datum):
    print "meindatum"
    return datetime.date(datum.year, datum.month, datum.day)


    HTH
     
    Bruno Desthuilliers, Mar 25, 2009
    #2
    1. Advertising

  3. Re: Does __init__ of subclass need the same argument types as __init__ of base class?

    Bruno Desthuilliers <>
    writes:

    >> The print command inside the __init__ method isn't executed, so that
    >> method doesn't seem to start at all.

    >
    > this often happens with (usually C-coded) immutable types. The
    > initializer is not called, only the "proper" constructor (__new__).


    More specifically, __init__ is called, but only after __new__ has
    finished, and __new__ gets the arguments passed in the constructor
    expression. Since __new__ raises an exception for those arguments,
    __init__ is never reached.

    The fix is in such cases is, as you point out, to override __new__ and
    not bother with __init__ at all.
     
    Hrvoje Niksic, Mar 25, 2009
    #3
  4. Bruno Desthuilliers schrieb:
    > Sibylle Koczian a écrit :
    > (snip)
    >>
    >>
    >> The print command inside the __init__ method isn't executed, so that
    >> method doesn't seem to start at all.

    >
    > this often happens with (usually C-coded) immutable types. The
    > initializer is not called, only the "proper" constructor (__new__). The
    > following should work (not tested):
    >
    > class Meindatum(datetime.date):
    > def __new__(self, datum):
    > print "meindatum"
    > return datetime.date(datum.year, datum.month, datum.day)
    >


    Thank you, that works, and I learned something (didn't know how Python
    objects are created). After some trial, error and searching on the
    Python website I found how to give Meindatum additional data attributes.
    Now it looks like this:

    class Sonderdatum(datetime.date):
    """
    Date with additional attribute (integer)
    """
    def __new__(cls, datum):
    print "Hier Sonderdatum.__new__"
    (x, y, z) = (datum.year, datum.month, datum.day)
    print "Jahr: %d, Monat: %d, Tag: %d" % (x, y, z)
    return super(Sonderdatum, cls).__new__(cls, datum.year,
    datum.month,
    datum.day)

    def __init__(self, datum, sonder=0):
    print "Hier Sonderdatum.__init__"
    # superclass __init__ is _not_ called!
    # super(Sonderdatum, self).__init__(x, y, z)
    self.sonder = sonder

    def testeSondertage():
    datum = datetime.datetime.strptime("01.01.2009", "%x")
    print repr(datum.date())
    xx = Sonderdatum(datum.date())
    print xx.year, xx.month, xx.day, xx.sonder

    if __name__ == "__main__":
    locale.setlocale(locale.LC_ALL, '')
    testeSondertage()

    And now I'll put this into something remotely useful.

    Je vous prie d'agréer mes meilleurs salutations,
    Sibylle
     
    Sibylle Koczian, Mar 25, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jstorta
    Replies:
    3
    Views:
    446
    jstorta
    Feb 20, 2006
  2. Steven Bethard
    Replies:
    2
    Views:
    458
    Steven Bethard
    Feb 16, 2005
  3. Kent Johnson
    Replies:
    7
    Views:
    915
    Jan Niklas Fingerle
    Feb 12, 2006
  4. Trans
    Replies:
    8
    Views:
    323
    Robert Klemme
    Oct 23, 2008
  5. Karan Rajput
    Replies:
    2
    Views:
    145
    Abinoam Jr.
    Dec 22, 2010
Loading...

Share This Page