inheriting from datetime

R

Rob Conner

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
********************************
 
G

Grant Edwards

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/comp.lang.python/browse_thread/thread/5f2517ecf6fb2ce/

You need to override __new__ rather than __init__
 
L

Lee Harr

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...


.... pass
....
Traceback (most recent call last):
.... pass
....Traceback (most recent call last):
datetime.datetime(1, 2, 3, 0, 0)



My guess is that the code you posted is not really the
code you were runnning.
 
R

rafi

Rob said:
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?
 
R

Rob Conner

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top