473,386 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Multiple inheritance and __slots__

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

Dec 14 '06 #1
5 3994
On 14 Dec 2006 05:23:33 -0800, jm*******@no.spam.gmail.com
<jm*******@gmail.comwrote:
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
si***@brunningonline.net
http://www.brunningonline.net/simon/blog/
Dec 14 '06 #2

Simon Brunning wrote:
On 14 Dec 2006 05:23:33 -0800, jm*******@no.spam.gmail.com
<jm*******@gmail.comwrote:
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
si***@brunningonline.net
http://www.brunningonline.net/simon/blog/
Dec 14 '06 #3
jm*******@no.spam.gmail.com 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

Dec 14 '06 #4
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
Dec 14 '06 #5
jm*******@no.spam.gmail.com wrote:
Simon Brunning wrote:
>On 14 Dec 2006 05:23:33 -0800, jm*******@no.spam.gmail.com
<jm*******@gmail.comwrote:
>>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
si***@brunningonline.net
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
Dec 14 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Nick Jacobson | last post by:
The __slots__ attribute of new-style classes can reduce memory usage when there are millions of instantiations of a class. So would a __slots__ attribute for functions/methods have a similar...
0
by: Jonathan Brandmeyer | last post by:
I am attempting to create a class that inherits from a PyGTK class and a Boost.Python-based class. When the interpreter executes code like: class Myclass( gtk.some_class, my_boost_python_class):...
1
by: Nitin Shukla | last post by:
Hello, Can anyone tell why am I getting this error and how to work around this problem. >>> class Klass(object): .... __slots__ = ( 'x' ) .... >>> class Klass1(object): .... __slots__ =...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.