Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Does __init__ of subclass need the same argument types as __init__of base class?

Reply
Thread Tools

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

 
 
Sibylle Koczian
Guest
Posts: n/a
 
      03-25-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@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

 
Reply With Quote
 
 
 
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      03-25-2009
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

 
Reply With Quote
 
 
 
 
Hrvoje Niksic
Guest
Posts: n/a
 
      03-25-2009
Bruno Desthuilliers <(E-Mail Removed)>
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.
 
Reply With Quote
 
Sibylle Koczian
Guest
Posts: n/a
 
      03-25-2009
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Should one always add super().__init__() to the __init__? Ramchandra Apte Python 17 09-30-2012 12:04 PM
__init__ in subclass of tuple Alan Isaac Python 2 03-10-2007 09:44 PM
subclass has a variable that is subclass of same superclass jstorta Java 3 02-20-2006 08:42 PM
super(...).__init__() vs Base.__init__(self) Kent Johnson Python 7 02-12-2006 08:59 PM
__new__ does not call __init__ as described in descrintro.html (WAS:Can __new__ prevent __init__ from being called?) Steven Bethard Python 2 02-16-2005 06:50 AM



Advertisments