multiple inheritance from list and other class

Discussion in 'Python' started by lars van gemerden, Jan 8, 2012.

  1. 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
    lars van gemerden, Jan 8, 2012
    #1
    1. Advertising

  2. 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
    Chris Angelico, Jan 8, 2012
    #2
    1. Advertising

  3. 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 <> 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.
    88888 Dihedral, Jan 8, 2012
    #3
  4. 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 <> 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.
    88888 Dihedral, Jan 8, 2012
    #4
  5. 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
    Steven D'Aprano, Jan 8, 2012
    #5
  6. 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
    Steven D'Aprano, Jan 8, 2012
    #6
  7. On Jan 8, 7:42 am, Steven D'Aprano <steve
    > 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
    lars van gemerden, Jan 8, 2012
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,755
    Thomas Weidenfeller
    Oct 12, 2005
  2. santosh
    Replies:
    3
    Views:
    5,387
    Mike Hewson
    Jan 5, 2005
  3. =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki

    Decralation of class inside other class and definition outside this class

    =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki, Jul 13, 2007, in forum: C++
    Replies:
    2
    Views:
    337
    Alf P. Steinbach
    Jul 13, 2007
  4. johnsonlau
    Replies:
    1
    Views:
    775
    Kai-Uwe Bux
    Jul 21, 2008
  5. Rouslan Korneychuk
    Replies:
    8
    Views:
    599
    Rouslan Korneychuk
    Feb 10, 2011
Loading...

Share This Page