Multi Heritage with slots

  • Thread starter Alexandre Badez
  • Start date
A

Alexandre Badez

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?
 
E

Eric Brunel

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?
 
A

Alexandre Badez

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?

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 ;)
 
S

Simon Brunning

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.
(e-mail address removed)
http://www.brunningonline.net/simon/blog/
GTalk: simon.brunning | MSN: small_values | Yahoo: smallvalues
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top