multiple inheritance from list and other class

  • Thread starter lars van gemerden
  • Start date
L

lars van gemerden

Hello,

I have an error message i do not understand:

My code is in essence:

class A(object):
#no __new__ or __init__
def meth1(self, args):
#some code
def meth2(self, args):
#some code

class B(list, A)
pass

b = B([1,2,3,4])

error:

Traceback (most recent call last):
File "D:\Documents\Code\Eclipse\workspace\FlowTools\iteraids.py",
line 431, in <module>
testolist()
File "D:\Documents\Code\Eclipse\workspace\FlowTools\iteraids.py",
line 381, in testolist
b = B([0,1,2,3,4])
TypeError: B() takes exactly 2 arguments (1 given)

(adapted the error to the example classes)

Can anyone help/explain?

Cheers, Lars
 
C

Chris Angelico

Hello,

I have an error message i do not understand:

My code is in essence:

b = B([1,2,3,4])

error:
   b = B([0,1,2,3,4])
TypeError: B() takes exactly 2 arguments (1 given)

Your code doesn't quite match your error message, and the code as
posted (with 'pass' in the method bodies to make it compile) works
fine. Can you derive a minimal test-case that actually produces the
error in question, and then post the actual code and error? Also -
what version of Python are you using?

Chris Angelico
 
8

88888 Dihedral

A list is a container.


Chris Angelicoæ–¼ 2012å¹´1月8日星期日UTC+8上åˆ9時27分06秒寫é“:
Hello,

I have an error message i do not understand:

My code is in essence:

b = B([1,2,3,4])

error:
   b = B([0,1,2,3,4])
TypeError: B() takes exactly 2 arguments (1 given)

Your code doesn't quite match your error message, and the code as
posted (with 'pass' in the method bodies to make it compile) works
fine. Can you derive a minimal test-case that actually produces the
error in question, and then post the actual code and error? Also -
what version of Python are you using?

Chris Angelico

The class is defined in a silly way.
In python declaring a class with only trivial properties added is
not very python at all.

Just let an object obtain new properties to save the troubles of
deriving a lot classes not different too much.
 
8

88888 Dihedral

A list is a container.


Chris Angelicoæ–¼ 2012å¹´1月8日星期日UTC+8上åˆ9時27分06秒寫é“:
Hello,

I have an error message i do not understand:

My code is in essence:

b = B([1,2,3,4])

error:
   b = B([0,1,2,3,4])
TypeError: B() takes exactly 2 arguments (1 given)

Your code doesn't quite match your error message, and the code as
posted (with 'pass' in the method bodies to make it compile) works
fine. Can you derive a minimal test-case that actually produces the
error in question, and then post the actual code and error? Also -
what version of Python are you using?

Chris Angelico

The class is defined in a silly way.
In python declaring a class with only trivial properties added is
not very python at all.

Just let an object obtain new properties to save the troubles of
deriving a lot classes not different too much.
 
S

Steven D'Aprano

On Sat, 07 Jan 2012 22:08:22 -0800, 88888 Dihedral wrote:

[...]
The class is defined in a silly way.
In python declaring a class with only trivial properties added is not
very python at all.

The example given looks like a Mixin class, which is perfectly acceptable
in Python.
 
S

Steven D'Aprano

Hello,

I have an error message i do not understand:

My code is in essence:

The code you give works fine. It does not show the error you say it does.
Please test your code before posting and ensure it actually fails the way
you expect.

It is perfectly fine to use multiple inheritance in the way you show.
Here is an even simpler example:

py> class Spam(object):
.... pass
....
py> class Ham(list, Spam):
.... pass
....
py>
py> h = Ham([1, 2, 3])
py>

And no exception is raised.
 
L

lars van gemerden

I have an error message i do not understand:
My code is in essence:

The code you give works fine. It does not show the error you say it does.
Please test your code before posting and ensure it actually fails the way
you expect.

It is perfectly fine to use multiple inheritance in the way you show.
Here is an even simpler example:

py> class Spam(object):
...     pass
...
py> class Ham(list, Spam):
...     pass
...
py>
py> h = Ham([1, 2, 3])
py>

And no exception is raised.

Sorry for wasting you time, I found the error (had "def" instead of
"class" before B in the example), sorry about not testing, shouldn't
post at 2:00 AM.

Thanks all the same, Lars
 

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,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top