473,401 Members | 2,125 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,401 software developers and data experts.

Overloading ctor doesn't work?

Hi there,

I just tried to run this code and failed miserably - though I dunno why.
Could any of you please enlighten me why this doesn't work?

Thanks a bunch.

--- snip ---
import unittest
from datetime import datetime

class time (datetime):
def __init__(self, hours=0, minutes=0, seconds=0, microseconds=0):
print "blah"
datetime.__init__(self, 1, 1, 1, hours, \
minutes, seconds, microseconds)
class Test (unittest.TestCase):
def testSmoke(self):
# print time() # bombs, and complains that
# the time ctor needs at least 3 arguments
self.assertEquals(datetime(1,1,1,1,2,3,4),time(1,2 ,3,4))
if __name__ == '__main__':
unittest.main()
--- snap ---

The reason I want to do this is that I want to work with times but I
want to do arithmetic with them. Therefore I cannot use the provided
time directly.

Now I thought, just overide the ctor of datetime so that year, month and
day are static and everything should work as far as I need it.

That is, it could work - though I seem to be unable to overide the ctor. :(

Why is that?

cu Martin

--
Reach me at spamfaenger (at) gmx (dot) net
Jul 18 '05 #1
9 1692
Martin Häcker wrote:
Hi there,

I just tried to run this code and failed miserably - though I dunno why.
Could any of you please enlighten me why this doesn't work?

Thanks a bunch.

--- snip ---
import unittest
from datetime import datetime

class time (datetime):
def __init__(self, hours=0, minutes=0, seconds=0, microseconds=0):
print "blah"
datetime.__init__(self, 1, 1, 1, hours, \
minutes, seconds, microseconds)
class Test (unittest.TestCase):
def testSmoke(self):
# print time() # bombs, and complains that
# the time ctor needs at least 3 arguments
self.assertEquals(datetime(1,1,1,1,2,3,4),time(1,2 ,3,4))
if __name__ == '__main__':
unittest.main()
--- snap ---

The reason I want to do this is that I want to work with times but I
want to do arithmetic with them. Therefore I cannot use the provided
time directly.

Now I thought, just overide the ctor of datetime so that year, month and
day are static and everything should work as far as I need it.

That is, it could work - though I seem to be unable to overide the ctor. :(

Why is that?

Perhpas the error message or output, which you don't provide, would
avoid our having to use our psychic powers? ;-)

In other words: what's wrong? How do you *know* you can't override it?

regards
Steve
--
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
Holden Web LLC +1 703 861 4237 +1 800 494 3119
Jul 18 '05 #2
> Martin Häcker wrote:
Hi there,

I just tried to run this code and failed miserably - though I dunno
why. Could any of you please enlighten me why this doesn't work?


Here is a simpler test case. I'm mystified too:

from datetime import datetime

class time (datetime):
def __init__(self, hours=0, minutes=0, seconds=0, microseconds=0):
datetime.__init__(self, 2001, 10, 31, hours, minutes, seconds, microseconds)

print time(1,2,3,4) # => 0001-02-03 04:00:00
print time() # => TypeError: function takes at least 3 arguments (0 given)
What happens to the default arguments to time.__init__? What happens to the 2001, 10, 31 arguments
to datetime.__init__?

I would expect the output to be
2001-10-31 01:02:03.000004
2001-10-31 00:00:00.000000

Kent
Jul 18 '05 #3
Hi,

It looks like the assertEquals use the != operator which had not been defined
to compare instances of your time class and instances of the datetime class.
In such a case, the operator ends up in comparing the references to instances,
i.e. the "id" of the objects, i.e. their physical memory addresses, which of
course can't be the same. And that is why your test fails.

Consider defining the __cmp__ method (see 2.3.3 Comparisons of the library
reference). Sess also the operator module.

Francis Girard
LANNILIS
Breizh
FRANCE

Le jeudi 20 Janvier 2005 19:23, Martin Häcker a écrit*:
Hi there,

I just tried to run this code and failed miserably - though I dunno why.
Could any of you please enlighten me why this doesn't work?

Thanks a bunch.

--- snip ---
import unittest
from datetime import datetime

class time (datetime):
def __init__(self, hours=0, minutes=0, seconds=0, microseconds=0):
print "blah"
datetime.__init__(self, 1, 1, 1, hours, \
minutes, seconds, microseconds)
class Test (unittest.TestCase):
def testSmoke(self):
# print time() # bombs, and complains that
# the time ctor needs at least 3 arguments
self.assertEquals(datetime(1,1,1,1,2,3,4),time(1,2 ,3,4))
if __name__ == '__main__':
unittest.main()
--- snap ---

The reason I want to do this is that I want to work with times but I
want to do arithmetic with them. Therefore I cannot use the provided
time directly.

Now I thought, just overide the ctor of datetime so that year, month and
day are static and everything should work as far as I need it.

That is, it could work - though I seem to be unable to overide the ctor. :(

Why is that?

cu Martin

--
Reach me at spamfaenger (at) gmx (dot) net


Jul 18 '05 #4
Wow !
Now, this is serious. I tried all sort of things but can't solve the problem.
I'm mystified too and forget my last reply.
I'm curious to see the answers.
Francis Girard
Le jeudi 20 Janvier 2005 19:59, Kent Johnson a écrit*:
Martin Häcker wrote:
Hi there,

I just tried to run this code and failed miserably - though I dunno
why. Could any of you please enlighten me why this doesn't work?


Here is a simpler test case. I'm mystified too:

from datetime import datetime

class time (datetime):
def __init__(self, hours=0, minutes=0, seconds=0, microseconds=0):
datetime.__init__(self, 2001, 10, 31, hours, minutes, seconds,
microseconds)

print time(1,2,3,4) # => 0001-02-03 04:00:00
print time() # => TypeError: function takes at least 3 arguments(0
given)
What happens to the default arguments to time.__init__? What happens to the
2001, 10, 31 arguments to datetime.__init__?

I would expect the output to be
2001-10-31 01:02:03.000004
2001-10-31 00:00:00.000000

Kent


Jul 18 '05 #5
"Kent Johnson" <ke******@yahoo.com> wrote in message
news:41**********@newspeer2.tds.net...
Martin Häcker wrote:
Hi there,

I just tried to run this code and failed miserably - though I dunno
why. Could any of you please enlighten me why this doesn't work?

Here is a simpler test case. I'm mystified too:

from datetime import datetime

class time (datetime):
def __init__(self, hours=0, minutes=0, seconds=0, microseconds=0):
datetime.__init__(self, 2001, 10, 31, hours, minutes, seconds,

microseconds)
print time(1,2,3,4) # => 0001-02-03 04:00:00
print time() # => TypeError: function takes at least 3 arguments (0 given)

What happens to the default arguments to time.__init__? What happens to the 2001, 10, 31 arguments to datetime.__init__?

I would expect the output to be
2001-10-31 01:02:03.000004
2001-10-31 00:00:00.000000

Kent


I can't explain this behavior, but this version does work (uses
datetime.combine instead of ctor)

-- Paul
from datetime import datetime, date as dt_date, time as dt_time

class time_d (datetime):
def __new__(cls, *args):
# default to no microseconds
if len(args)==3:
args = args + (0,)
if len(args)==4:
tmpdate = datetime.today()
h, mi, s, ms = args
return datetime.combine(tmpdate, dt_time(h,mi,s,ms))
elif len(args)==7:
y,m,d,h,mi,s,ms = args
return datetime.combine(dt_date(y,m,d), dt_time(h,mi,s,ms))
else:
raise TypeError, "wrong number of args"

print time_d(2001,10,31,1,2,3,4)
print time_d(1,2,3,4)
print time_d(1,2,3)

Jul 18 '05 #6
Paul McGuire wrote:
"Kent Johnson" <ke******@yahoo.com> wrote in message
news:41**********@newspeer2.tds.net...
Martin Häcker wrote:
Hi there,

I just tried to run this code and failed miserably - though I dunno
why. Could any of you please enlighten me why this doesn't work?
Here is a simpler test case. I'm mystified too:

from datetime import datetime

class time (datetime):
def __init__(self, hours=0, minutes=0, seconds=0, microseconds=0):
datetime.__init__(self, 2001, 10, 31, hours, minutes, seconds,


microseconds)
print time(1,2,3,4) # => 0001-02-03 04:00:00
print time() # => TypeError: function takes at least 3 arguments (0


given)

What happens to the default arguments to time.__init__? What happens to


the 2001, 10, 31 arguments
to datetime.__init__?

I would expect the output to be
2001-10-31 01:02:03.000004
2001-10-31 00:00:00.000000

Kent

I can't explain this behavior, but this version does work (uses
datetime.combine instead of ctor)

-- Paul
from datetime import datetime, date as dt_date, time as dt_time

class time_d (datetime):
def __new__(cls, *args):
# default to no microseconds
if len(args)==3:
args = args + (0,)
if len(args)==4:
tmpdate = datetime.today()
h, mi, s, ms = args
return datetime.combine(tmpdate, dt_time(h,mi,s,ms))
elif len(args)==7:
y,m,d,h,mi,s,ms = args
return datetime.combine(dt_date(y,m,d), dt_time(h,mi,s,ms))
else:
raise TypeError, "wrong number of args"

print time_d(2001,10,31,1,2,3,4)
print time_d(1,2,3,4)
print time_d(1,2,3)

Ah, right. The light turns on...

datetime is immutable so overriding the constructor doesn't change the constructed object. You have
to override __new__ instead.
http://www.python.org/2.2.1/descrintro.html#__new__

This works:

from datetime import datetime

class time (datetime):
def __new__(cls, hours=0, minutes=0, seconds=0, microseconds=0):
return datetime.__new__(cls, 2001, 10, 31, hours, minutes, seconds, microseconds)

print time(1,2,3,4) # => 2001-10-31 01:02:03.000004
print time() # => 2001-10-31 00:00:00

Kent


Jul 18 '05 #7
> Ah, right. The light turns on...

datetime is immutable so overriding the constructor doesn't change the
constructed object. You have to override __new__ instead.
http://www.python.org/2.2.1/descrintro.html#__new__


Ahhh! Thanks a bunch, now this makes things much clearer.

Thanks again!
cu Martin

--
Reach me at spamfaenger (at) gmx (dot) net
Jul 18 '05 #8
Martin Häcker <dw***@hotmail.com> wrote:
Now I thought, just overide the ctor of datetime so that year, month and
day are static and everything should work as far as I need it.

That is, it could work - though I seem to be unable to overide the ctor. :(

Why is that?


Its a bug!

http://sourceforge.net/tracker/index...70&atid=105470

However its been fixed in a recent Python 2.3.

(I was bitten by the same thing which used to fail but now works after
an upgrade of python 2.3!)
--
Nick Craig-Wood <ni**@craig-wood.com> -- http://www.craig-wood.com/nick
Jul 18 '05 #9
Nick Craig-Wood wrote:
Martin Häcker <dw***@hotmail.com> wrote:
Now I thought, just overide the ctor of datetime so that year, month and
day are static and everything should work as far as I need it.

That is, it could work - though I seem to be unable to overide the ctor. :(
Its a bug!

http://sourceforge.net/tracker/index...70&atid=105470

However its been fixed in a recent Python 2.3.


My example was developed in Python 2.4. The problem was the immutability of datetime.

Kent

(I was bitten by the same thing which used to fail but now works after
an upgrade of python 2.3!)

Jul 18 '05 #10

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

Similar topics

18
by: Joe Seigh | last post by:
Is there a good write on this. The textbooks I have fluff over on this? Specifically, I trying to dereference with 2 levels of type conversion not 1, i.e. X<T> -> z => Y<T> -> z => T* ->...
3
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. I am...
20
by: KL | last post by:
I am working on a school assignment, so please don't tell me the solution. I just want some direction. I am supposed to overload the >, <, ==, !=, >=, and <= operators using bool. I am having...
39
by: zeus | last post by:
I know function overloading is not supported in C. I have a few questions about this: 1. Why? is it from technical reasons? if so, which? 2. why wasn't it introduced to the ANSI? 3. Is there any...
31
by: | last post by:
Hi, Why can I not overload on just the return type? Say for example. public int blah(int x) { }
7
by: Eric Sabine | last post by:
Here should be an easy question. I want to overload the ctor of the class with 2 versions, one takes 3 input parameters and the other takes 2, if I call the "2" version I want to simply call the 3...
6
by: TuxC0d3 | last post by:
Hi! I'm diving into the some more ++ specific aspects of c++ (and finally accepting that c++ is more than "a plus added to c" :), so that means using namespaces, templates, std::strings, lists,...
3
by: toton | last post by:
Operator overloading has a sort syntax rather than member function call for stack based memory allocation. like complex<int> c1,c2,c3; c3= c1+c2; How the same can be applied to heap based...
13
by: jehugaleahsa | last post by:
Hello: Today I learned that the + operator cannot be passed a delegate. I get an error from the CLR saying I have an invalid program. With that, I was wondering if someone could tell me what...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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,...

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.