Multiple inheritance and __slots__

Discussion in 'Python' started by jm.suresh@no.spam.gmail.com, Dec 14, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. On 14 Dec 2006 05:23:33 -0800,
    <> 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

    http://www.brunningonline.net/simon/blog/
     
    Simon Brunning, Dec 14, 2006
    #2
    1. Advertising

  3. Guest

    Simon Brunning wrote:
    > On 14 Dec 2006 05:23:33 -0800,
    > <> 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
    >
    > http://www.brunningonline.net/simon/blog/
     
    , Dec 14, 2006
    #3
  4. 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/Cookbook/Python/Recipe/252158 (how
    to freeze Python classes)

    Michele Simionato
     
    Michele Simionato, Dec 14, 2006
    #4
  5. greg Guest

    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
     
    greg, Dec 14, 2006
    #5
  6. Larry Bates Guest

    wrote:
    > Simon Brunning wrote:
    >> On 14 Dec 2006 05:23:33 -0800,
    >> <> 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
    >>
    >> 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
     
    Larry Bates, Dec 14, 2006
    #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. Jp Calderone
    Replies:
    1
    Views:
    525
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
    Jul 6, 2003
  2. Metaclass and __slots__

    , Nov 6, 2003, in forum: Python
    Replies:
    1
    Views:
    316
    Peter Otten
    Nov 6, 2003
  3. Porky Pig Jr
    Replies:
    7
    Views:
    328
    Porky Pig Jr
    Jun 3, 2004
  4. Nitin Shukla

    Multiple Inheritance __slots__ problem

    Nitin Shukla, Dec 24, 2004, in forum: Python
    Replies:
    1
    Views:
    559
    Alex Martelli
    Dec 24, 2004
  5. Rouslan Korneychuk
    Replies:
    8
    Views:
    613
    Rouslan Korneychuk
    Feb 10, 2011
Loading...

Share This Page