Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #13002 > unrolled thread

TypeError: object.__init__() takes no parameters

Started byOliver <wildeskraut@googlemail.com>
First post2011-09-08 22:47 -0700
Last post2011-09-09 13:31 -0400
Articles 9 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  TypeError: object.__init__() takes no parameters Oliver <wildeskraut@googlemail.com> - 2011-09-08 22:47 -0700
    Re: TypeError: object.__init__() takes no parameters Ben Finney <ben+python@benfinney.id.au> - 2011-09-09 16:44 +1000
    Re: TypeError: object.__init__() takes no parameters Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2011-09-09 08:57 +0200
      Re: TypeError: object.__init__() takes no parameters Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-09-09 18:33 +1000
        Re: TypeError: object.__init__() takes no parameters Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2011-09-09 11:11 +0200
        Re: TypeError: object.__init__() takes no parameters Duncan Booth <duncan.booth@invalid.invalid> - 2011-09-09 13:20 +0000
          Re: TypeError: object.__init__() takes no parameters Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-09-09 23:41 +1000
      Re: TypeError: object.__init__() takes no parameters Duncan Booth <duncan.booth@invalid.invalid> - 2011-09-09 09:49 +0000
    Re: TypeError: object.__init__() takes no parameters Terry Reedy <tjreedy@udel.edu> - 2011-09-09 13:31 -0400

#13002 — TypeError: object.__init__() takes no parameters

FromOliver <wildeskraut@googlemail.com>
Date2011-09-08 22:47 -0700
SubjectTypeError: object.__init__() takes no parameters
Message-ID<e4d24c0f-3556-420d-bfed-4b14927d35a4@v16g2000prb.googlegroups.com>
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 :)

[toc] | [next] | [standalone]


#13005

FromBen Finney <ben+python@benfinney.id.au>
Date2011-09-09 16:44 +1000
Message-ID<87sjo6mcyy.fsf@benfinney.id.au>
In reply to#13002
Oliver <wildeskraut@googlemail.com> writes:

> let me be honest with you, I am a poor programmer who can only do
> Perl.

I strongly suspect you could do more than that.

Welcome to the Python discussion forum.

> 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.

Then he can hardly complain if someone else re-writes it in their
language of choice, can he? :-)

> C:\Documents and Settings\mhg\Desktop\palcalc>shapes.py
> EE...

This, and the rest of your output, is from the call to ‘unittest.main’,
Python's standard-library module for unit testing.

>   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

The ‘super(Container, self)’ call asks for a ‘super’ object representing
the (next) superclass of the type of ‘self’.

In this case, as the error message indicates, the next superclass type
is ‘object’ – the base type of all objects. Also as the error message
indicates, the initialiser for ‘object’ doesn't accept the parameters
being passed.

> 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

So, there's no sense in calling the superclass's ‘__init__’ method with
parameters which won't be accepted because of the function signature.


The next thing to do is to ask about the design of these classes (what
is the intention of this initialiser?). But you say you're entirely new
to it, and that the designer has no interest in it. What are your
options for getting their interest and participating in this thread?

-- 
 \     “I cannot conceive that anybody will require multiplications at |
  `\   the rate of 40,000 or even 4,000 per hour …” —F. H. Wales, 1936 |
_o__)                                                                  |
Ben Finney

[toc] | [prev] | [next] | [standalone]


#13006

FromThomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de>
Date2011-09-09 08:57 +0200
Message-ID<j4cdda$f61$1@r03.glglgl.eu>
In reply to#13002
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

[toc] | [prev] | [next] | [standalone]


#13009

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2011-09-09 18:33 +1000
Message-ID<4e69cf6d$0$29996$c3e8da3$5496439d@news.astraweb.com>
In reply to#13006
Thomas Rachel wrote:

> 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.

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("two", "parameters")
<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.



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#13011

FromThomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de>
Date2011-09-09 11:11 +0200
Message-ID<j4cl7r$k72$1@r03.glglgl.eu>
In reply to#13009
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? :-(

[toc] | [prev] | [next] | [standalone]


#13025

FromDuncan Booth <duncan.booth@invalid.invalid>
Date2011-09-09 13:20 +0000
Message-ID<Xns9F5B906CD9148duncanbooth@127.0.0.1>
In reply to#13009
Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote:

> Thomas Rachel wrote:
> 
>> 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.
> 
> 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("two", "parameters")
><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.
> 
More recently than that. It only became an error in 2.6:

[dbooth@localhost ~]$ python2.5 -c "object().__init__(42)"
[dbooth@localhost ~]$ python2.6 -c "object().__init__(42)"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: object.__init__() takes no parameters


-- 
Duncan Booth http://kupuguy.blogspot.com

[toc] | [prev] | [next] | [standalone]


#13026

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2011-09-09 23:41 +1000
Message-ID<4e6a1791$0$29984$c3e8da3$5496439d@news.astraweb.com>
In reply to#13025
Duncan Booth wrote:

> Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote:

>> In Python 2.2, the default object constructor accepts, and ignores, any
>> parameters. In Python 2.3 on up, that becomes an error.
>> 
> 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.



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#13013

FromDuncan Booth <duncan.booth@invalid.invalid>
Date2011-09-09 09:49 +0000
Message-ID<Xns9F5B6D522BDAFduncanbooth@127.0.0.1>
In reply to#13006
Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915
@spamschutz.glglgl.de> wrote:

> 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__().
> 
It isn't nonsense, just poor and outdated practice. You might subclass 
Container with something that puts another base class into the mro between 
Container and object and in that case calling super(Container, self).
__init__() would be wrong.

object.__init__() used to accept and silently ignore any parameters. This 
meant you could safely pass any constructor parameters along in case there 
was an intervening base class that wanted them. However that was changed 
(presumably after this code was written) and now you have to be a bit more 
careful with your initialisation parameters.

This would be better in general, though if there is actually any code 
depending on xsize,ysize being passed up to a base class it will still need 
changing at the call site:

    def __init__(self, xsize=1200, ysize=800, *args, **kw):
          super(Container, self).__init__(*args, **kw)

That way any xsize and ysize arguments are removed but any additional 
arguments for other base classes are passed through.

-- 
Duncan Booth http://kupuguy.blogspot.com

[toc] | [prev] | [next] | [standalone]


#13031

FromTerry Reedy <tjreedy@udel.edu>
Date2011-09-09 13:31 -0400
Message-ID<mailman.909.1315589550.27778.python-list@python.org>
In reply to#13002
On 9/9/2011 1:47 AM, Oliver wrote:

> 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

-- 
Terry Jan Reedy

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web