473,403 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 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 1392
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: John Hunter | last post by:
>>> from datetime import date >>> dt = date(1005,1,1) >>> print dt.strftime('%Y') Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: year=1005 is before 1900; the...
2
by: BJörn Lindqvist | last post by:
A problem I have occured recently is that I want to subclass builtin types. Especially subclassing list is very troublesome to me. But I can't find the right syntax to use. Take for example this...
9
by: rick cameron | last post by:
TimeZone - the only instance you can get is one representing the local time zone DateTime - cannot represent a time before 00:00:00 1 Jan 1 CE What were they thinking? This is a serious question...
5
by: Pieter Linden | last post by:
Hi, This question refers sort of to Rebecca Riordan's article on Access web about subclassing entities: http://www.mvps.org/access/tables/tbl0013.htm How practical is this? I am writing a...
1
by: Kevin | last post by:
Hi All I am having a problem retrieving a Date value from an Access 2000 database using the OLEDbProvider. Can someone tell me please how to create a DateTime Object with the date fro m the...
11
by: Axel Dahmen | last post by:
Hi, I'm working in a team developing an international website. When I set the page's CultureInfo to Hungarian (hu-HU) then the RangeValidator doesn't seem to be able to parse a TextBox's date...
2
by: Paulo da Silva | last post by:
Hi! What's wrong with this way of subclassing? from datetime import date class MyDate(date): def __init__(self,year,month=None,day=None): if type(year) is str: # The whole date is here as...
1
by: Mike Rooney | last post by:
Hi everyone, this is my first post to this list. I am trying to create a subclass of datetime.date and pickle it, but I get errors on loading it back. I have created a VERY simple demo of this: ...
11
by: =?Utf-8?B?UGFvbG8=?= | last post by:
I have a SQL database table with rows in which the primary key is a DateTime type (although I don't use the Time part). Have added numerous rows and now want to delete one so I enter the date...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.