TypeError: object.__init__() takes no parameters

O

Oliver

Hello together,

let me be honest with you, I am a poor programmer who can only do
Perl.
I tried to code a program in Perl, but found that another guy already
finished this job in Python.
Unlucky for me, this guy is now CEO of a company, not really
interested in supporting his old code.

If I want to run shapes.py I receive this error message:

=== error message ===

C:\Documents and Settings\mhg\Desktop\palcalc>shapes.py
EE...
======================================================================
ERROR: test_fits (__main__.containerTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Documents and Settings\mhg\Desktop\palcalc\shapes.py", line
265, in test_fits
cont = Container()
File "C:\Documents and Settings\mhg\Desktop\palcalc\shapes.py", line
90, in __init__
super(Container, self).__init__(xsize, ysize)
TypeError: object.__init__() takes no parameters

======================================================================
ERROR: test_place_bin (__main__.containerTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Documents and Settings\mhg\Desktop\palcalc\shapes.py", line
257, in test_place_bin
cont = Container()
File "C:\Documents and Settings\mhg\Desktop\palcalc\shapes.py", line
90, in __init__
super(Container, self).__init__(xsize, ysize)
TypeError: object.__init__() takes no parameters

----------------------------------------------------------------------
Ran 5 tests in 0.032s

FAILED (errors=2)

=== end of error message ===

Snippets of source code:

Line 264 - 268

def test_fits(self):
cont = Container()
cont.place_bin(0, 0, 210, 280)
self.assertEqual(False, cont.fits(Rectangle(0, 210, 280,
420)))
self.assertEqual(True, cont.fits(Rectangle(0, 280, 280, 490)))

Line 87 - 93

class Container(object):
"""Container to store a number of non-overlapping rectangles."""
def __init__(self, xsize=1200, ysize=800):
super(Container, self).__init__(xsize, ysize)
self.rect = Rectangle(0, 0, xsize, ysize)
self.boxes = []
self.last_placement_strategy = 0

Line 254 - 262

class containerTests(unittest.TestCase):
"""Tests for container objects."""
def test_place_bin(self):
cont = Container()
cont.place_bin(0, 0, 40, 40)
self.failUnlessRaises(RuntimeError, cont.place_bin, 0, 0, 40,
40)
cont = Container()
cont.place_bin(0, 0, 210, 280)
self.failUnlessRaises(RuntimeError, cont.place_bin, 0, 210,
280, 420)

I think this is the main function:

if __name__ == '__main__':
unittest.main()

I do not know if I posted what you need to help me, it just would ease
my life a lot. If you need more information please let me know - if
you want you can even be unfriendly to me :)
 
T

Thomas Rachel

Am 09.09.2011 07:47 schrieb Oliver:
class Container(object):
"""Container to store a number of non-overlapping rectangles."""
def __init__(self, xsize=1200, ysize=800):
super(Container, self).__init__(xsize, ysize)

And this is the nonsense: Container derives from object and tries to
call its superclass's constructor with 2 parameters - which won't work.

Write this as super(Container, self).__init__().

HTH,

Thomas
 
S

Steven D'Aprano

Thomas said:
Am 09.09.2011 07:47 schrieb Oliver:

And this is the nonsense: Container derives from object and tries to
call its superclass's constructor with 2 parameters - which won't work.

Not nonsense. Merely a backward-incompatible change:


[steve@sylar ~]$ python2.2
Python 2.2.3 (#1, Aug 12 2010, 01:08:27)
[GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2
Type "help", "copyright", "credits" or "license" for more information.<object object at 0x92a43e8>


In Python 2.2, the default object constructor accepts, and ignores, any
parameters. In Python 2.3 on up, that becomes an error.
 
T

Thomas Rachel

Am 09.09.2011 10:33 schrieb Steven D'Aprano:
Not nonsense. Merely a backward-incompatible change:
[1]


In Python 2.2, the default object constructor accepts, and ignores, any
parameters. In Python 2.3 on up, that becomes an error.

Thanks, I wasn't aware of that. My first contact with Python was with 2.3...

Thomas

[1] Another example why backward-incompatible change are bad. Why was it
necessary in this case? :-(
 
S

Steven D'Aprano

Duncan said:
More recently than that. It only became an error in 2.6:

For __init__, sure, but it was an error for __new__ back in 2.3. Sorry for
not being more clear.
 
T

Terry Reedy

If I want to run shapes.py I receive this error message:

Others have explained why code that ran once now does not.
class Container(object):
"""Container to store a number of non-overlapping rectangles."""
def __init__(self, xsize=1200, ysize=800):
super(Container, self).__init__(xsize, ysize)

Remove this line and the error will go away. It *never* did anything.
self.rect = Rectangle(0, 0, xsize, ysize)
self.boxes = []
self.last_placement_strategy = 0
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top