Multi Heritage with slots

Discussion in 'Python' started by Alexandre Badez, Sep 5, 2007.

  1. Hye,

    I'm developing a little app, and I want to make multi heritage.
    My problem is that my both parent do have __slots__ define.

    So I've got something like:

    class foo(object):
    __slots__ = ['a', 'b']
    pass

    class foo2(object):
    __slots__ = ['c', 'd']
    pass

    class crash(foo, foo2):
    pass

    If you write only that in a sample file or in python console (as I
    did), python refuse to load the module and report something like:

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    TypeError: Error when calling the metaclass bases
    multiple bases have instance lay-out conflict

    Do you know why it append? And how could I make this work?
     
    Alexandre Badez, Sep 5, 2007
    #1
    1. Advertising

  2. Alexandre Badez

    Eric Brunel Guest

    On Wed, 05 Sep 2007 11:01:56 +0200, Alexandre Badez
    <> wrote:

    > Hye,
    >
    > I'm developing a little app, and I want to make multi heritage.
    > My problem is that my both parent do have __slots__ define.
    >
    > So I've got something like:
    >
    > class foo(object):
    > __slots__ = ['a', 'b']
    > pass
    >
    > class foo2(object):
    > __slots__ = ['c', 'd']
    > pass
    >
    > class crash(foo, foo2):
    > pass
    >
    > If you write only that in a sample file or in python console (as I
    > did), python refuse to load the module and report something like:
    >
    > Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > TypeError: Error when calling the metaclass bases
    > multiple bases have instance lay-out conflict
    >
    > Do you know why it append? And how could I make this work?


    See http://mail.python.org/pipermail/python-list/2006-December/418768.html

    Basically, the general advice you're likely to get here is: don't use
    __slots__, or at least don't use __slots__ with inheritance.

    BTW, what are you trying to do? Is it really a memory footprint
    optimization, which is the intended use case for __slots__, or are you
    just doing Java in Python?
    --
    python -c "print ''.join([chr(154 - ord(c)) for c in
    'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"
     
    Eric Brunel, Sep 5, 2007
    #2
    1. Advertising

  3. On Sep 5, 12:42 pm, "Eric Brunel" <> wrote:
    > Seehttp://mail.python.org/pipermail/python-list/2006-December/418768.html
    >
    > Basically, the general advice you're likely to get here is: don't use
    > __slots__, or at least don't use __slots__ with inheritance.
    >
    > BTW, what are you trying to do? Is it really a memory footprint
    > optimization, which is the intended use case for __slots__, or are you
    > just doing Java in Python?
    > --
    > python -c "print ''.join([chr(154 - ord(c)) for c in
    > 'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"


    Thanks for your answer.
    I use __slots__ not for memory optimization nor doing java.
    I use __slots__ because my class are used by other lib, and in the
    past, some of them misspell some attributes and involved a very
    annoying comportment of the global application.
    So the objective is only to prevent unwanted dynamism (but not in all
    the application, just some class).

    PS: I very like your signature ;)
     
    Alexandre Badez, Sep 5, 2007
    #3
  4. On 9/5/07, Alexandre Badez <> wrote:
    > I use __slots__ not for memory optimization nor doing java.
    > I use __slots__ because my class are used by other lib, and in the
    > past, some of them misspell some attributes and involved a very
    > annoying comportment of the global application.
    > So the objective is only to prevent unwanted dynamism (but not in all
    > the application, just some class).


    Using slots to prevent the creation of new properties is what Eric
    *means* by "doing java". You're trying to write Java style code in
    Python, and it's not going to be pretty. Slots are not intended for
    this purpose, and they aren't very good for it.

    --
    Cheers,
    Simon B.

    http://www.brunningonline.net/simon/blog/
    GTalk: simon.brunning | MSN: small_values | Yahoo: smallvalues
     
    Simon Brunning, Sep 5, 2007
    #4
  5. On Sep 5, 2:52 pm, "Simon Brunning" <> wrote:
    > On 9/5/07, Alexandre Badez <> wrote:
    >
    > > I use __slots__ not for memory optimization nor doing java.
    > > I use __slots__ because my class are used by other lib, and in the
    > > past, some of them misspell some attributes and involved a very
    > > annoying comportment of the global application.
    > > So the objective is only to prevent unwanted dynamism (but not in all
    > > the application, just some class).

    >
    > Using slots to prevent the creation of new properties is what Eric
    > *means* by "doing java". You're trying to write Java style code in
    > Python, and it's not going to be pretty. Slots are not intended for
    > this purpose, and they aren't very good for it.
    >


    Right, and this the way to do what the original poster wants:

    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/252158

    Michele Simionato
     
    Michele Simionato, Sep 5, 2007
    #5
  6. On Sep 5, 2:56 pm, Michele Simionato <>
    wrote:
    > On Sep 5, 2:52 pm, "Simon Brunning" <> wrote:
    >
    > > On 9/5/07, Alexandre Badez <> wrote:

    >
    > > > I use __slots__ not for memory optimization nor doing java.
    > > > I use __slots__ because my class are used by other lib, and in the
    > > > past, some of them misspell some attributes and involved a very
    > > > annoying comportment of the global application.
    > > > So the objective is only to prevent unwanted dynamism (but not in all
    > > > the application, just some class).

    >
    > > Using slots to prevent the creation of new properties is what Eric
    > > *means* by "doing java". You're trying to write Java style code in
    > > Python, and it's not going to be pretty. Slots are not intended for
    > > this purpose, and they aren't very good for it.

    >
    > Right, and this the way to do what the original poster wants:
    >
    > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/252158
    >
    > Michele Simionato


    Thanks every body for your remarks.
    I still have a lot to learn in python...
     
    Alexandre Badez, Sep 5, 2007
    #6
    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. marco

    dynamic import with heritage

    marco, Jun 10, 2004, in forum: Python
    Replies:
    8
    Views:
    596
    marco
    Jun 11, 2004
  2. tin gherdanarra

    slots? SLOTS?

    tin gherdanarra, Oct 12, 2005, in forum: Python
    Replies:
    2
    Views:
    2,328
    Peter Hansen
    Oct 13, 2005
  3. bugzilla
    Replies:
    15
    Views:
    1,420
    Greg Comeau
    Mar 28, 2005
  4. Zero
    Replies:
    8
    Views:
    507
  5. John Pritchard-williams

    Japanese Heritage of Ruby: useful by-product?....

    John Pritchard-williams, Aug 28, 2008, in forum: Ruby
    Replies:
    4
    Views:
    138
    Dido Sevilla
    Aug 29, 2008
Loading...

Share This Page