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 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 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>
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>
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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.
...
|
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
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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....
|
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$) {
}
...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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,...
| |