Multiple inheritance and __slots__

J

jm.suresh

Hi all,
From the google search, it seems its not possible to do the following.
.... __slots__ = ['a']
........ __slots__ = ['b']
........ __slots__ = ['c']
....
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Error when calling the metaclass bases
multiple bases have instance lay-out conflict

I just want to make sure that I am using only the attributes a,b and c
from the instances of Test3 . Is there any other hack that could be
done.
 
S

Simon Brunning

Hi all,
From the google search, it seems its not possible to do the following.
... __slots__ = ['a']
...... __slots__ = ['b']
...... __slots__ = ['c']
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Error when calling the metaclass bases
multiple bases have instance lay-out conflict

I just want to make sure that I am using only the attributes a,b and c
from the instances of Test3 . Is there any other hack that could be
done.

Difficulty with subclassing is the price you pay for abusing slots.
Slots are intended as a performance tweak only, to minimise the memory
footprint of classes of which you are going to have a great number of
instances.

In short - don't do that.
 
J

jm.suresh

Simon said:
Hi all,
From the google search, it seems its not possible to do the following.
class Test1(object):
... __slots__ = ['a']
...
class Test2(object):
... __slots__ = ['b']
...
class Test3(Test1,Test2):
... __slots__ = ['c']
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Error when calling the metaclass bases
multiple bases have instance lay-out conflict

I just want to make sure that I am using only the attributes a,b and c
from the instances of Test3 . Is there any other hack that could be
done.

Difficulty with subclassing is the price you pay for abusing slots.
Slots are intended as a performance tweak only, to minimise the memory
footprint of classes of which you are going to have a great number of
instances.

In short - don't do that.
OK. But is there any other way to do what __slots__ does as a 'side
effect' i.e. forcing me to think about the list of attributes my class
is going to have upfront and raising error whenever I violate it. IMHO
this is a very good thing to have even if one does not care about
memory.
 
G

greg

Simon said:
Difficulty with subclassing is the price you pay for abusing slots.

Although you could have the same difficulty even
if you weren't abusing them.

It's just a limitation of the implementation.
The use of __slots__ forces a particular layout
im memory, and you can only do that for one
base class at a time.
 
L

Larry Bates

Simon said:
Hi all,
From the google search, it seems its not possible to do the following.

class Test1(object):
... __slots__ = ['a']
...
class Test2(object):
... __slots__ = ['b']
...
class Test3(Test1,Test2):
... __slots__ = ['c']
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Error when calling the metaclass bases
multiple bases have instance lay-out conflict

I just want to make sure that I am using only the attributes a,b and c
from the instances of Test3 . Is there any other hack that could be
done.
Difficulty with subclassing is the price you pay for abusing slots.
Slots are intended as a performance tweak only, to minimise the memory
footprint of classes of which you are going to have a great number of
instances.

In short - don't do that.
OK. But is there any other way to do what __slots__ does as a 'side
effect' i.e. forcing me to think about the list of attributes my class
is going to have upfront and raising error whenever I violate it. IMHO
this is a very good thing to have even if one does not care about
memory.

--
Suresh
--
Cheers,
Simon B
(e-mail address removed)
http://www.brunningonline.net/simon/blog/

Sounds a lot like you are coming from another programming language
and are trying to make Python act like it did. Hey I did the same
thing when I first took up Python as a language. Python is not Java
(or any other language that puts you in a straight jacket). IMHO if
you embrace the dynacism of Python and you will be much happier
writing code in it. Don't worry if someone will try to assign to
some attribute in your class that "is illegal". They may be doing
if for some reason you can't fathom at the outset.

-Larry
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top