471,119 Members | 1,366 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,119 software developers and data experts.

Re: Subclassing datetime.date does not seem to work

Rick King schrieb:
I would like to subclass datetime.date so that I can write:

d = date2('12312008')

I tried:

from datetime import date
class date2(date):
def __init__( self, strng ):
mm,dd,yy = int(strng[:2]), int(strng[2:4]), int(strng[4:])
date.__init__(self,yy,mm,dd)

But then this statement:
d = date2('12312008')

Causes:
TypeError: function takes exactly 3 arguments (1 given)

Is there something basically wrong with subclassing date?
-Rick King
datetime.date is a C extension class. Subclassing of extension classes
may not always work as you'd expect it.

Christian
Jun 27 '08 #1
1 1294
On Apr 26, 7:43 am, Christian Heimes <li...@cheimes.dewrote:
Rick King schrieb:
I would like to subclass datetime.date so that I can write:
d = date2('12312008')
I tried:
from datetime import date
class date2(date):
def __init__( self, strng ):
mm,dd,yy = int(strng[:2]), int(strng[2:4]), int(strng[4:])
date.__init__(self,yy,mm,dd)
But then this statement:
d = date2('12312008')
Causes:
TypeError: function takes exactly 3 arguments (1 given)
Is there something basically wrong with subclassing date?
-Rick King

datetime.date is a C extension class. Subclassing of extension classes
may not always work as you'd expect it.
.... and in this case it's a sledgehammer to crack a nut:
>>from datetime import date
def date_from_string(strng):
.... mm, dd, yy = int(strng[:2]), int(strng[2:4]), int(strng[4:])
.... return date(yy, mm, dd)
....
>>date_from_string('12312008')
datetime.date(2008, 12, 31)
>>>
Consider also:
>>import datetime
datetime.datetime.strptime('12312008', '%m%d%Y').date()
datetime.date(2008, 12, 31)

Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by John Hunter | last post: by
2 posts views Thread by BJörn Lindqvist | last post: by
5 posts views Thread by Pieter Linden | last post: by
1 post views Thread by Kevin | last post: by
2 posts views Thread by Paulo da Silva | last post: by
1 post views Thread by Mike Rooney | last post: by
11 posts views Thread by =?Utf-8?B?UGFvbG8=?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.