Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Multiple inheritance and __slots__

Reply
Thread Tools

Multiple inheritance and __slots__

 
 
jm.suresh@no.spam.gmail.com
Guest
Posts: n/a
 
      12-14-2006
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.

--
Suresh

 
Reply With Quote
 
 
 
 
Simon Brunning
Guest
Posts: n/a
 
      12-14-2006
On 14 Dec 2006 05:23:33 -0800, http://www.velocityreviews.com/forums/(E-Mail Removed)
<(E-Mail Removed)> wrote:
> 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.

--
Cheers,
Simon B
(E-Mail Removed)
http://www.brunningonline.net/simon/blog/
 
Reply With Quote
 
 
 
 
jm.suresh@no.spam.gmail.com
Guest
Posts: n/a
 
      12-14-2006

Simon Brunning wrote:
> On 14 Dec 2006 05:23:33 -0800, (E-Mail Removed)
> <(E-Mail Removed)> wrote:
> > 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 Removed)
> http://www.brunningonline.net/simon/blog/


 
Reply With Quote
 
Michele Simionato
Guest
Posts: n/a
 
      12-14-2006
(E-Mail Removed) wrote:
> 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.


See http://aspn.activestate.com/ASPN/Coo.../Recipe/252158 (how
to freeze Python classes)

Michele Simionato

 
Reply With Quote
 
greg
Guest
Posts: n/a
 
      12-14-2006
Simon Brunning wrote:

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

--
Greg
 
Reply With Quote
 
Larry Bates
Guest
Posts: n/a
 
      12-14-2006
(E-Mail Removed) wrote:
> Simon Brunning wrote:
>> On 14 Dec 2006 05:23:33 -0800, (E-Mail Removed)
>> <(E-Mail Removed)> wrote:
>>> 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 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
 
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
Multiple Inheritance __slots__ problem Nitin Shukla Python 1 12-24-2004 01:48 PM
a question on __slots__ (and example from Nutshell) Porky Pig Jr Python 7 06-03-2004 08:04 PM
Metaclass and __slots__ anabell@sh163.net Python 1 11-06-2003 08:09 AM
tp_base, tp_basesize, and __slots__ instance __class__ reassignment Jp Calderone Python 1 07-06-2003 07:17 AM



Advertisments