471,336 Members | 1,273 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,336 software developers and data experts.

Why can't I subclass off of "date" ?

Ok, maybe this is a stupid question, but why can't I make a subclass of
datetime.date and override the __init__ method?

---

from datetime import date

class A(date):
def __init__(self, a, b, c, d):
print a, b, c, d
date.__init__(self, 2006, 12, 11)

d = A(1, 2, 3, 4)

---

$ python break_date.py
Traceback (most recent call last):
File "break_date.py", line 9, in ?
d = A(1, 2, 3, 4)
TypeError: function takes exactly 3 arguments (4 given)
If I make A a subclass of some toy class that is constructed with three
arguments, it works fine. Why can't I make "date" the subclass?

Thanks for any advice.

David

Aug 24 '06 #1
3 1462
da**********@gmail.com wrote:
Ok, maybe this is a stupid question, but why can't I make a subclass of
datetime.date and override the __init__ method?

---

from datetime import date

class A(date):
def __init__(self, a, b, c, d):
print a, b, c, d
date.__init__(self, 2006, 12, 11)

d = A(1, 2, 3, 4)

---

$ python break_date.py
Traceback (most recent call last):
File "break_date.py", line 9, in ?
d = A(1, 2, 3, 4)
TypeError: function takes exactly 3 arguments (4 given)
If I make A a subclass of some toy class that is constructed with three
arguments, it works fine. Why can't I make "date" the subclass?
You'll have to also override the __new__ method.

Georg
Aug 24 '06 #2
da**********@gmail.com wrote:
Ok, maybe this is a stupid question, but why can't I make a subclass of
datetime.date and override the __init__ method?
__init__ controls initialization of an already constructed object. to
control construction, you need to override __new__:

http://pyref.infogami.com/__new__

e.g.

class A(date):
def __new__(cls, a, b, c, d):
print a, b, c, d
return super(A, cls).__new__(cls, 2006, 12, 11)

</F>

Aug 24 '06 #3
Wow, you guys are fast.

Thanks Georg and Fredrik!!

Fredrik Lundh wrote:
da**********@gmail.com wrote:
Ok, maybe this is a stupid question, but why can't I make a subclass of
datetime.date and override the __init__ method?

__init__ controls initialization of an already constructed object. to
control construction, you need to override __new__:

http://pyref.infogami.com/__new__

e.g.

class A(date):
def __new__(cls, a, b, c, d):
print a, b, c, d
return super(A, cls).__new__(cls, 2006, 12, 11)

</F>
Aug 24 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Westcoast Sheri | last post: by
13 posts views Thread by Dr John Stockton | last post: by
5 posts views Thread by Martien van Wanrooij | last post: by

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.