473,385 Members | 1,712 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,385 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 1546
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Westcoast Sheri | last post by:
When I used the "date("nj")" function to generate an invoice, about 1 out of 500 times, nothing comes up <?php $invoicedate = date("nj"); ?> <html><head></head><body> Invoice date: <?php...
0
by: charlesb | last post by:
I have a query that works in the rest of the SQL world "SELECT invoice.*, invoice.Date FROM invoice WHERE (DateDiff("m", Date, Now())=1);" which will give me all of last month's invoices. ...
2
by: ITM | last post by:
Does anyone have an example of an SQL query which returns rows for the year-to-date, but where the "year" commences on August 1st? e.g. select * from mytable where datefield > last august 1st ...
13
by: Dr John Stockton | last post by:
Javascript date strings can have a one-letter postfix; it is taken as indicating time zone (not J, which causes NaN). // IIRC, VB accepts A & P in that location, for AM & PM. In my MS IE 4, the...
5
by: Martien van Wanrooij | last post by:
I would like to retrieve, let us say, the First Monday after a certain date, so my (imaginary) function could be something like echo weekdayAfter("28 July 2005", "Monday") should return "1 August...
5
by: bg | last post by:
Hi! How do I check if "date" exists before using that code? I've built a RSSreader and sometimes there's a date in it and sometimes not. How can I check if it exists to avoid crash...
2
by: devprog | last post by:
objConn = New ADODB.Connection objConn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbPath _ & ";Jet OLEDB:Database Password=" & dbPassword) This cause error: (VB.NET. ) sqlString...
12
by: Emi Lu | last post by:
Hello all, I have a question about "date" & "timestamp" types in PostgreSQL. I want to setup the default value '0000-00-00' and "0000-00-00 00:00:00" for them. However, it seems that PostgreSQL...
0
by: Curious | last post by:
Hi, I have two columns defined as DateTime type in the Visual Designer, i.e., Dataset.xsd. In the grid to which the columns are bound, they're both displayed as date, for instance, 5/23/2007....
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
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,...

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.