469,658 Members | 1,666 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,658 developers. It's quick & easy.

Subclassing: what is wrong here?

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 a string
year,month,day=map(int,string.split(year,'-'))
if year<100:
year+=2000
date.__init__(self,year,month,day)

When I do
d=MyDate("2007-3-15")
I got
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: function takes exactly 3 arguments (1 given)

Thanks for any help.
Mar 16 '07 #1
2 1105
Hello Paulo,
What's wrong with this way of subclassing?
...
See http://sourceforge.net/tracker/index...70&atid=105470

HTH,
--
Miki <mi*********@gmail.com>
http://pythonwise.blogspot.com

Mar 16 '07 #2
Miki escreveu:
Hello Paulo,
>What's wrong with this way of subclassing?
...
See http://sourceforge.net/tracker/index...70&atid=105470

HTH,
--
Miki <mi*********@gmail.com>
http://pythonwise.blogspot.com
Thanks!
It works this way:

class MyDate(date):
def __new__(cls,year,month=None,day=None):
if type(year) is str:
# The whole date is here as a string
year,month,day=map(int,string.split(year,'-'))
if year<100:
year+=2000
return date.__new__(cls,year,month,day)

Mar 16 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Peter Olsen | last post: by
11 posts views Thread by Brent | last post: by
5 posts views Thread by Pieter Linden | last post: by
2 posts views Thread by Alexander Wehrli | last post: by
1 post views Thread by alex23 | last post: by
4 posts views Thread by Erik Johnson | last post: by
16 posts views Thread by manatlan | last post: by
1 post views Thread by Christian Heimes | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.