[ python-Bugs-1448640 ] datetime.__init__ cannot be overridden
SourceForge.net
noreply at sourceforge.net
Sun Mar 19 13:25:21 CET 2006
Bugs item #1448640, was opened at 2006-03-13 04:54
Message generated for change (Comment added) made by gbrandl
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1448640&group_id=5470
Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Library
Group: None
Status: Closed
Resolution: Invalid
Priority: 5
Submitted By: Martin Blais (blais)
Assigned to: Michael Hudson (mwh)
Summary: datetime.__init__ cannot be overridden
Initial Comment:
Hi
The following code does not work properly:
#!/usr/bin/env python
"""
Test overriding constructor of datetime classes.
"""
import sys, datetime
class MyDate(datetime.date):
def __init__( self, year, month, day ):
print >> sys.stderr, 'lose data'
d = MyDate(2006, 11, 29)
print d
class MyDate(datetime.date):
def __new__( self, year, month, day ):
print 'lose data'
def __init__( self, year, month, day ):
print 'lose data again'
d = MyDate(2006, 11, 29)
print d
The output is:
lose data
2006-11-29
lose data
None
The problem is that the initialization of the object is
done in its time_new() method which is registered for
__new__ rather than using an __init__ method. This
prevent one from overriding the date/datetime/time
constructors.
cheers,
----------------------------------------------------------------------
>Comment By: Georg Brandl (gbrandl)
Date: 2006-03-19 12:25
Message:
Logged In: YES
user_id=849994
blais: your mistake was that you didn't call
datetime.date.__new__() in your overridden __new__() and did
return None from __new__ whereas __new__ must return a new
object of type MyDate.
----------------------------------------------------------------------
Comment By: Martin Blais (blais)
Date: 2006-03-13 21:45
Message:
Logged In: YES
user_id=10996
Hmmmm... that's not quite true. If I derive from datetime
in order to add new data members (e.g. in my case I add
"seq" which is used as a sequence number on top of datetime
for storing the names of photographs, the sequence number is
in case I have a panorama or multiple photographs in burst
mode--all within one second), I might want a different
constructor.
I tried overriding __new__ and had some troubles, cannot
remember what exactly, will reproduce and send code soon.
----------------------------------------------------------------------
Comment By: Michael Hudson (mwh)
Date: 2006-03-13 16:42
Message:
Logged In: YES
user_id=6656
datetime.date objects are immutable, so this is the same as not being able to
override __init__ in a subclass of int. Override __new__ instead.
----------------------------------------------------------------------
Comment By: splitscreen (splitscreen)
Date: 2006-03-13 15:40
Message:
Logged In: YES
user_id=1126061
Isn't this an abuse of __new__ ?
Taken from the documentation:
"__new__ must return an object. There's nothing that
requires that it return a new object that is an instance of
its class argument, although that is the convention. If you
return an existing object, the constructor call will still
call its __init__ method. If you return an object of a
different class, its __init__ method will be called. If you
forget to return something, Python will unhelpfully return
None, and your caller will probably be very confused."
So, you're actually calling __init__ with None?
Or have i misunderstood?
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1448640&group_id=5470
More information about the Python-bugs-list
mailing list