Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 499939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:02:11+00:00 2026-05-13T06:02:11+00:00

In the Python data model reference section on slots there is a list of

  • 0

In the Python data model reference section on slots there is a list of notes on using __slots__. I am thoroughly confused by the 1st and 6th items, because they seem to be contradicting each other.

First item:

  • When inheriting from a class without
    __slots__, the __dict__ attribute
    of that class will always be
    accessible, so a __slots__
    definition in the subclass is
    meaningless.

Sixth item:

  • The action of a __slots__
    declaration is limited to the class
    where it is defined. As a result,
    subclasses will have a __dict__
    unless they also define __slots__
    (which must only contain names of any
    additional slots).

It seems to me these items could be better worded or shown through code, but I have been trying to wrap my head around this and am still coming up confused. I do understand how __slots__ are supposed to be used, and I am trying to get a better grasp on how they work.

The Question:

Can someone please explain to me in plain language what the conditions are for inheritance of slots when subclassing?

(Simple code examples would be helpful but not necessary.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T06:02:11+00:00Added an answer on May 13, 2026 at 6:02 am

    As others have mentioned, the sole reason for defining __slots__ is to save some memory, when you have simple objects with a predefined set of attributes and don’t want each to carry around a dictionary. This is meaningful only for classes of which you plan to have many instances, of course.

    The savings may not be immediately obvious — consider…:

    >>> class NoSlots(object): pass
    ... 
    >>> n = NoSlots()
    >>> class WithSlots(object): __slots__ = 'a', 'b', 'c'
    ... 
    >>> w = WithSlots()
    >>> n.a = n.b = n.c = 23
    >>> w.a = w.b = w.c = 23
    >>> sys.getsizeof(n)
    32
    >>> sys.getsizeof(w)
    36
    

    From this, it would seem the with-slots size is larger than the no-slots size! But that’s a mistake, because sys.getsizeof doesn’t consider "object contents" such as the dictionary:

    >>> sys.getsizeof(n.__dict__)
    140
    

    Since the dict alone takes 140 bytes, clearly the "32 bytes" object n is alleged to take are not considering all that’s involved in each instance. You can do a better job with third-party extensions such as pympler:

    >>> import pympler.asizeof
    >>> pympler.asizeof.asizeof(w)
    96
    >>> pympler.asizeof.asizeof(n)
    288
    

    This shows much more clearly the memory footprint that’s saved by __slots__: for a simple object such as this case, it’s a bit less than 200 bytes, almost 2/3 of the object’s overall footprint. Now, since these days a megabyte more or less doesn’t really matter all that much to most applications, this also tells you that __slots__ is not worth the bother if you’re going to have just a few thousand instances around at a time — however, for millions of instances, it sure does make a very important difference. You can also get a microscopic speedup (partly due to better cache use for small objects with __slots__):

    $ python -mtimeit -s'class S(object): __slots__="x","y"' -s's=S(); s.x=s.y=23' 's.x'
    10000000 loops, best of 3: 0.37 usec per loop
    $ python -mtimeit -s'class S(object): pass' -s's=S(); s.x=s.y=23' 's.x'
    1000000 loops, best of 3: 0.604 usec per loop
    $ python -mtimeit -s'class S(object): __slots__="x","y"' -s's=S(); s.x=s.y=23' 's.x=45'
    1000000 loops, best of 3: 0.28 usec per loop
    $ python -mtimeit -s'class S(object): pass' -s's=S(); s.x=s.y=23' 's.x=45'
    1000000 loops, best of 3: 0.332 usec per loop
    

    but this is somewhat dependent on Python version (these are the numbers I measure repeatably with 2.5; with 2.6, I see a larger relative advantage to __slots__ for setting an attribute, but none at all, indeed a tiny disadvantage, for getting it).

    Now, regarding inheritance: for an instance to be dict-less, all classes up its inheritance chain must also have dict-less instances. Classes with dict-less instances are those which define __slots__, plus most built-in types (built-in types whose instances have dicts are those on whose instances you can set arbitrary attributes, such as functions). Overlaps in slot names are not forbidden, but they’re useless and waste some memory, since slots are inherited:

    >>> class A(object): __slots__='a'
    ... 
    >>> class AB(A): __slots__='b'
    ... 
    >>> ab=AB()
    >>> ab.a = ab.b = 23
    >>> 
    

    as you see, you can set attribute a on an AB instance — AB itself only defines slot b, but it inherits slot a from A. Repeating the inherited slot isn’t forbidden:

    >>> class ABRed(A): __slots__='a','b'
    ... 
    >>> abr=ABRed()
    >>> abr.a = abr.b = 23
    

    but does waste a little memory:

    >>> pympler.asizeof.asizeof(ab)
    88
    >>> pympler.asizeof.asizeof(abr)
    96
    

    so there’s really no reason to do it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 384k
  • Answers 384k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The answer was to add the tag within the tag.… May 14, 2026 at 11:10 pm
  • Editorial Team
    Editorial Team added an answer glGet with GL_BLEND_SRC and GL_BLEND_DST (and glIsEnabled(GL_BLEND), if you're not… May 14, 2026 at 11:10 pm
  • Editorial Team
    Editorial Team added an answer Why not store the month names in a HashSet? This… May 14, 2026 at 11:10 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.