[ python-Bugs-1448640 ] datetime.__init__ cannot be overridden

SourceForge.net noreply at sourceforge.net
Mon Mar 13 16:40:07 CET 2006


Bugs item #1448640, was opened at 2006-03-13 04:54
Message generated for change (Comment added) made by splitscreen
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: Open
Resolution: None
Priority: 5
Submitted By: Martin Blais (blais)
Assigned to: Nobody/Anonymous (nobody)
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: 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