473,387 Members | 1,516 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,387 software developers and data experts.

inheriting from datetime

So this is simple, why can't I run the following code? I've tried many
variances of this, but simply cannot inherit from datetime or
datetime.datetime. I get this on line 3.
TypeError: function takes at most 2 arguments (3 given)

********************************
import datetime
_datetime = datetime.datetime

class MyDateTime(_datetime):
"""
Identical to builtin datetime.datetime, except it accepts
invalid dates and times as input.
"""
_valid = True

def __init__(self, year, month, day, *args, **kw):
try:
_datetime.__init__(self, year, month, day, *args, **kw)
except _datetime.ValueError:
_valid = False
self.year = year
self.month = month
self.day = day
self.args = args
self.kw = kw
********************************

Aug 1 '05 #1
4 2709
On 2005-08-01, Rob Conner <rt******@gmail.com> wrote:
So this is simple, why can't I run the following code? I've tried many
variances of this, but simply cannot inherit from datetime or
datetime.datetime. I get this on line 3.
TypeError: function takes at most 2 arguments (3 given)


I think I posted this question a while back and the problem was
that a datetime instance is not mutable, so the normal method
of deriving a class didn't work....

A quick google...

http://groups-beta.google.com/group/...2517ecf6fb2ce/

You need to override __new__ rather than __init__

--
Grant Edwards grante Yow! I'm ANN LANDERS!! I
at can SHOPLIFT!!
visi.com
Aug 1 '05 #2
On 2005-08-01, Rob Conner <rt******@gmail.com> wrote:
So this is simple, why can't I run the following code? I've tried many
variances of this, but simply cannot inherit from datetime or
datetime.datetime. I get this on line 3.
TypeError: function takes at most 2 arguments (3 given)

********************************
import datetime
_datetime = datetime.datetime

class MyDateTime(_datetime):
"""
Identical to builtin datetime.datetime, except it accepts
invalid dates and times as input.
"""
_valid = True

def __init__(self, year, month, day, *args, **kw):
try:
_datetime.__init__(self, year, month, day, *args, **kw)
except _datetime.ValueError:
_valid = False
self.year = year
self.month = month
self.day = day
self.args = args
self.kw = kw
********************************


It helps to post the actual code and error message you
are seeing...
import datetime
class foo(datetime): .... pass
....
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: function takes at most 2 arguments (3 given) class foo(datetime.datetime): .... pass
.... d = foo() Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: function takes at least 3 arguments (0 given) d = foo(1, 2, 3)
d foo(1, 2, 3, 0, 0) datetime.datetime(1, 2, 3)

datetime.datetime(1, 2, 3, 0, 0)

My guess is that the code you posted is not really the
code you were runnning.
Aug 1 '05 #3
Rob Conner wrote:
So this is simple, why can't I run the following code? I've tried many
variances of this, but simply cannot inherit from datetime or
datetime.datetime. I get this on line 3.
TypeError: function takes at most 2 arguments (3 given)


line 3 of the following code or line 3 of the use of the code?

on my box it runs, except that ValueError is not caught by the except.

side question: what is the point of accepting invalid dates?

--
rafi

"Imagination is more important than knowledge."
(Albert Einstein)
Aug 1 '05 #4
gah, yeah that was strange. but i got it now. thanks.
side question: what is the point of accepting invalid dates?

thats a long story. but it would be nice to have invalid dates at least
just stored. so i want to try to put a class together that does it.

Aug 1 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: richardlane | last post by:
Hi, basic question here - I'm struggling with the transfer from asp to asp.net a bit, especially in seeing the 'bigger picture' of how things are best structured. I have a website made up...
15
by: JustSomeGuy | last post by:
this doesn't want to compile.... class image : public std::list<element> { element getElement(key k) const { image::iterator iter; for (iter=begin(); iter != end(); ++iter) { element...
29
by: shaun roe | last post by:
I want something which is very like a bitset<64> but with a couple of extra functions: set/get the two 32 bit words, and conversion to unsigned long long. I can do this easily by inheriting from...
1
by: john diss | last post by:
hello there everyone.. I have created a class called "ProcessLog" inheriting from XmlDocument and two classes ("UploadedItem", "ProcessError") inheriting from XmlElement. I then have two...
11
by: Noah Coad [MVP .NET/C#] | last post by:
How do you make a member of a class mandatory to override with a _new_ definition? For example, when inheriting from System.Collections.CollectionBase, you are required to implement certain...
1
by: luminousc | last post by:
Hi guys, I'm still a relative newbie at oop, as my background has been in old fashioned vanilla ASP, so please try to make your responses as simple as possible. I'm having issues with...
2
by: Charles Law | last post by:
I want a set of controls that all have a border, like a group box. I thought I would create a base control containing just a group box from which my set of controls could inherit. Assuming that...
3
by: Alex Satrapa | last post by:
There's some mention in the (old!) documentation that constraints such as foreign keys won't include data from inheriting tables, eg: CREATE TABLE foo ( id SERIAL PRIMARY KEY ); CREATE TABLE...
5
by: UJ | last post by:
I have a user control that inherits from the standard windows text box. I have a routine that adds stuff to it. I have compiled it into a dll. I have added it to my app and I can debug into it. I...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.