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.

Re: TypeError with date class

Hi,

Le Friday 13 June 2008 15:24:31 Dummy Pythonese Luser, vous avez écritÂ*:
Greetings *.*:

The following program caused an error and puzzled me to no end. Any help
immensely appreciated.
class Second(First):
def __init__(self, datestr):
y = int(datestr[0:4])
m = int(datestr[4:6])
d = int(datestr[6:8])
First.__init__(self, y, m, d)

# a = Second("20060201")
# TypeError: function takes exactly 3 arguments (1 given)
# Why?
probably because date is an immutable type, so the init stuff is done in
__new__ and not __init__ (and thus __new__ expects the same args as
__init__). So you should replace First and Second __init__ methods by
something like this :
def __new__ (cls, datestr):
y = int(datestr[0:4])
m = int(datestr[4:6])
d = int(datestr[6:8])
self = date.__new__(cls, y, m, d)
return self
The general rule is to do your initialization in __new__ for immutable types
and in __init__ for mutable ones. See the chapter 3 of the reference manual
(data model) for more infos.

--
Cédric Lucantis
Jun 27 '08 #1
0 666

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

Similar topics

1
by: Atul Kshirsagar | last post by:
Hello, I am using Python 2.3.2 with a C++ extention DLL in muti-threaded environment. 1. For each new thread I create a separate sub-interpreter. 2. Each thread executes multiple python...
4
by: Manoj | last post by:
Hi , I have written a simple program usinga class. But it not compiling.The error i get is my = myclass("Paul", "John") TypeError: this constructor takes no arguments I checked some previous...
7
by: ‘5ÛHH575-UAZWKVVP-7H2H48V3 | last post by:
(see end of message for example code) When an instance has a dynamically assigned instance method, deepcopy throws a TypeError with the message "TypeError: instancemethod expected at least 2...
5
by: Gerard Flanagan | last post by:
Hello all Could anyone shed any light on the following Exception? The code which caused it is below. Uncommenting the 'super' call in 'XmlNode' gives the same error. If I make XmlNode a...
1
by: Paul Du Bois | last post by:
Using win32 python 2.4.1, I have a minimal test program: def generate(): raise TypeError('blah') yield "" print "\n".join((generate())) Executing the program gives:
42
by: Holger | last post by:
Hi guys Tried searching for a solution to this, but the error message is so generic, that I could not get any meaningfull results. Anyways - errormessage:...
8
by: digitalorganics | last post by:
What are the reason one would get this error: TypeError: Cannot create a consistent method resolution order (MRO) for bases object ?? I can provide the code if needed....
2
by: kath | last post by:
Hi, the following shows the contents of "datebook.xls" Date 8/9/2006 8/9/2006 8/9/2006 8/9/2006 8/9/2006
33
by: christophertidy | last post by:
Hi I am new to Python and have recieved this error message when trying to instantiate an object from a class from another file within the same directory and wondered what I have done wrong. I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.