Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > multiple inheritance from list and other class

Reply
Thread Tools

multiple inheritance from list and other class

 
 
lars van gemerden
Guest
Posts: n/a
 
      01-08-2012
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\ite raids.py",
line 431, in <module>
testolist()
File "D:\Documents\Code\Eclipse\workspace\FlowTools\ite raids.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



 
Reply With Quote
 
 
 
 
Chris Angelico
Guest
Posts: n/a
 
      01-08-2012
On Sun, Jan 8, 2012 at 12:16 PM, lars van gemerden <> wrote:
> 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
 
Reply With Quote
 
 
 
 
88888 Dihedral
Guest
Posts: n/a
 
      01-08-2012
A list is a container.


Chris Angelico於 2012年1月8日星期日UTC+8上午9時27分06秒 道:
> On Sun, Jan 8, 2012 at 12:16 PM, lars van gemerden <la...@rational-it.com> wrote:
> > 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.
 
Reply With Quote
 
88888 Dihedral
Guest
Posts: n/a
 
      01-08-2012
A list is a container.


Chris Angelico於 2012年1月8日星期日UTC+8上午9時27分06秒 道:
> On Sun, Jan 8, 2012 at 12:16 PM, lars van gemerden <la...@rational-it.com> wrote:
> > 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.
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      01-08-2012
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.



--
Steven
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      01-08-2012
On Sat, 07 Jan 2012 17:16:22 -0800, lars van gemerden wrote:

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


--
Steven
 
Reply With Quote
 
lars van gemerden
Guest
Posts: n/a
 
      01-08-2012
On Jan 8, 7:42*am, Steven D'Aprano <steve
+comp.lang.pyt...@pearwood.info> wrote:
> On Sat, 07 Jan 2012 17:16:22 -0800, lars van gemerden wrote:
> > 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.
>
> --
> Steven


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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
inheritance, multiple inheritance and the weaklist and instance dictionaries Rouslan Korneychuk Python 8 02-10-2011 04:02 AM
C++ Struct inheritance against class inheritance johnsonlau C++ 1 07-21-2008 04:58 PM
Decralation of class inside other class and definition outside this class =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki C++ 2 07-12-2007 11:52 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Difference Between List x; and List x(); , if 'List' is a Class? roopa C++ 6 08-27-2004 06:18 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57