GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — __slots__ for subclasses of variable length types · Issue #41779 · python/cpython

__slots__ for subclasses of variable length types #41779

New issue
Copy link
New issue
Copy link
Closed
Closed
__slots__ for subclasses of variable length types#41779
Copy link
Assignees
arigo
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancementA feature request or enhancement

Description

@mwhudson
mwhudson
opened on Mar 30, 2005
Issue body actions
BPO 1173475
Nosy @mwhudson, @rhettinger, @terryjreedy, @ronaldoussoren, @devdanzin, @vadmium
Files
  • var-sized-slots-1.diff: mwh's patch #1
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/arigo'
    closed_at = <Date 2010-08-11.22:03:01.354>
    created_at = <Date 2005-03-30.17:09:14.000>
    labels = ['interpreter-core', 'type-feature']
    title = '__slots__ for subclasses of variable length types'
    updated_at = <Date 2015-03-12.05:25:41.393>
    user = 'https://github.com/mwhudson'

    bugs.python.org fields:

    activity = <Date 2015-03-12.05:25:41.393>
    actor = 'martin.panter'
    assignee = 'arigo'
    closed = True
    closed_date = <Date 2010-08-11.22:03:01.354>
    closer = 'rhettinger'
    components = ['Interpreter Core']
    creation = <Date 2005-03-30.17:09:14.000>
    creator = 'mwh'
    dependencies = []
    files = ['6581']
    hgrepos = []
    issue_num = 1173475
    keywords = []
    message_count = 12.0
    messages = ['48094', '48095', '48096', '48097', '48098', '82173', '110432', '113454', '113615', '113630', '113631', '184641']
    nosy_count = 7.0
    nosy_names = ['mwh', 'rhettinger', 'terry.reedy', 'ronaldoussoren', 'ajaksu2', 'BreamoreBoy', 'martin.panter']
    pr_nums = []
    priority = 'normal'
    resolution = 'wont fix'
    stage = 'patch review'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue1173475'
    versions = ['Python 3.4']

    Linked PRs

    • gh-41779: Allow defining the __dict__ and __weakref__ __slots__ for any class #141755
    • gh-41779: Allow defining any __slots__ for a class derived from tuple #141763

    Activity

    1. mwhudson commented on Mar 30, 2005

      @mwhudson
      mwhudson
      on Mar 30, 2005
      Author
      More actions

      This is a first, rough cut at allowing subclasses of variable length
      types to have __slots__ of all flavours, not just __dict__.

      The motivation is trying to understand and document what's going on
      in typeobject.c, and the less special cases knocking around the
      better.

      This patch also allows instances of such classes to be weakly
      referenced.

      What is missing: tests, lots of tests, documentation. Also, the code
      is a bit hackish at various points; a degree of clean up can certainly
      be acheived.

      Also, I think my code probably fails to cope with code like:

      class A(str):
       pass # implicitly adds __dict__, __weakref__
      class B(A):
       __slots__ = ["a", "b"]
      
      b = B()
      b.c = 1

      Hmm, yes. Oh well, no time to fix today (I don't think it's that big a
      deal).

    2. mwhudson
      assigned
      arigo
      on Mar 30, 2005
    3. mwhudson
      added
      interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
      on Mar 30, 2005
    4. arigo commented on Apr 3, 2005

      @arigo
      arigomannequin
      on Apr 3, 2005
      Mannequin
      More actions

      Logged In: YES
      user_id=4771

      I'm confused: the rule for negative slot offsets appear to be different to the one for tp_dictoffset, which only increases the amount of obscurity around here.

      tp_dictoffset counts relative to the end of the object, whereas in your patch negative slot offsets are a different trick to mean "relative to the start but skipping the varsized part". The difference shows up when subclassing increases tp_basicsize. This should be resolved one way or the other -- and I think that a clear picture of the various parts of the object and how they are measured would be a good start.

      That's also related to your proposed change to extra_ivars(), which would become slightly more permissive; I strongly suspect that it would allow more strange segfaulting cases to sneak in undetected...

    5. mwhudson commented on Apr 3, 2005

      @mwhudson
      mwhudson
      on Apr 3, 2005
      Author
      More actions

      Logged In: YES
      user_id=6656

      I'm confused: the rule for negative slot offsets appear to be
      different to the one for tp_dictoffset

      Yes. I think this is actually necessary.

      Consider:

      class S(str):
          __slots__ = ['a']

      you'd except S.__dict__['a'].__offset__ (well, if the attribute existed) to be
      -4.

      Then

      class T(S):
          __slots__ = ['b']

      then using the 'from the end of the object' rule for T().a would actually find
      T.b. (IOW, T.__dict__['b'].__offset__ == T.__dict__['a'].__offset__ == -4).
      The alternative would be to somehow override all the slots in S when T is
      defined -- and this doesn't seem wise.

      __dict__ indeed works differently, because
      instance.__class__.__dictoffset__ is updated on subclassing. You could
      make __dict__ work like the slots mentioned above, but then you'd have to
      find the '__dict__' descriptor every time you wanted to access an
      instance's dictionary, and that would be slow (and might even not really
      work, but I don't want to risk brain-explosion by thinking about it too hard)

      which only increases the amount of obscurity around here.

      Yeah, sorry about that.

      I think something I've realised over the past few days is that __dict__
      really is special. I'm not sure __weakref__ is (though I guess it's special in
      that you want to be able to access it from C without any risk of executing
      Python level code, i.e. replacing Py_GETWEAKREFLIST(ob) with
      PyOjbect_GetAttrString(ob, "__weakref__") would be unfortunate).

      This should be resolved one way or the other

      See above -- don't think you can.

      -- and I think that
      a clear picture of the various parts of the object and how they
      are measured would be a good start.

      No kidding here!

      That's also related to your proposed change to extra_ivars(),
      which would become slightly more permissive; I strongly suspect
      that it would allow more strange segfaulting cases to sneak in
      undetected...

      Almost certainly!

    6. arigo commented on Apr 3, 2005

      @arigo
      arigomannequin
      on Apr 3, 2005
      Mannequin
      More actions

      Logged In: YES
      user_id=4771

      I think it's still possible to give slot.offset the same meaning as tp_dictoffset, even given the additional constrain that it can't change upon subclassing. In your example classes S and T, we can put 'b' before 'a' in memory, so that a.offset==-4 (for both S and T) and b.offset==-8.

    7. mwhudson commented on Apr 3, 2005

      @mwhudson
      mwhudson
      on Apr 3, 2005
      Author
      More actions

      Logged In: YES
      user_id=6656

      Heh, yes that works, and completely hadn't occurred to me.

    8. devdanzin commented on Feb 15, 2009

      @devdanzin
      devdanzinmannequin
      on Feb 15, 2009
      Mannequin
      More actions

      Patch has tests.

    9. devdanzin
      added
      type-featureA feature request or enhancementA feature request or enhancement
      on Feb 15, 2009
    10. BreamoreBoy commented on Jul 16, 2010

      @BreamoreBoy
      BreamoreBoymannequin
      on Jul 16, 2010
      Mannequin
      More actions

      How much rework if any is needed to get this patch into py3k?

    11. terryjreedy commented on Aug 9, 2010

      @terryjreedy
      terryjreedy
      on Aug 9, 2010
      Member
      More actions

      I believe this is covered by the PEP-3003 3.2 change moratorium.

    12. rhettinger commented on Aug 11, 2010

      @rhettinger
      rhettinger
      on Aug 11, 2010
      Contributor
      More actions

      ISTM the space saving of value of __slots__ isn't typically needed in the context of variable length built-in types.

      Guido has long regarded __slots__ as a confusing hack. That should warn us away for extending its functionality.

      Unless there are some compelling use cases, a simple and clean patch, and a clear one sentence explanation for users, I recommend this feature request be closed.

      Michael, do you still want this?

    13. mwhudson commented on Aug 11, 2010

      @mwhudson
      mwhudson
      on Aug 11, 2010
      Author
      More actions

      Well, I can think of some counters to that -- surely it's _more_ confusing if slots only works some of the time? -- but realistically I'm not going to work on this any further.

    14. rhettinger commented on Aug 11, 2010

      @rhettinger
      rhettinger
      on Aug 11, 2010
      Contributor
      More actions

      Declaring YAGNI and closing.
      Thanks Michael.

    15. rhettinger
      closed this as completedon Aug 11, 2010
    16. ronaldoussoren commented on Mar 19, 2013

      @ronaldoussoren
      ronaldoussoren
      on Mar 19, 2013
      Contributor
      More actions

      I do have a usecase for this: subclasses of int.

      Having slots would be nice for a reasonably efficient implementation of named constants (as recently discussed on python-ideas), and I'm already using a subclass of int of PyObjC to attach a single other value to a Python integer. In both cases the overhead of the __dict__ is pretty large.

    17. ezio-melotti
      transferred this issue fromon Apr 9, 2022
    18. serhiy-storchaka commented on Dec 30, 2024

      @serhiy-storchaka
      serhiy-storchaka
      on Dec 30, 2024
      Member
      More actions

      I have a use case for this for namedtuple subclasses, in particular to add fields to urllib.parse classes (#67041).

    19. serhiy-storchaka
      reopened this on Dec 30, 2024
    20. serhiy-storchaka
      added 5 commits that reference this issue on Nov 19, 2025

      pythongh-41779: Allow defining the __dict__ and __weakref__ __slots__…

      b42a86a

      gh-41779: Allow defining the __dict__ and __weakref__ __slots__ for a…

      4bcab46

      pythongh-41779: Allow defining the __dict__ and __weakref__ __slots__…

      6865073

      pythongh-41779: Allow defining any __slots__ for a class derived from…

      009f8e2

      pythongh-41779: Allow defining any __slots__ for a class derived from…

      a452fce
    21. StanFromIreland
      added a commit that references this issue on Dec 6, 2025

      pythongh-41779: Allow defining the __dict__ and __weakref__ __slots__…

      b4b4f60
    22. ashm-dev
      added a commit that references this issue on Dec 8, 2025

      pythongh-41779: Allow defining the __dict__ and __weakref__ __slots__…

      abda153
    23. serhiy-storchaka
      added a commit that references this issue on Jan 6, 2026

      gh-41779: Allow defining any __slots__ for a class derived from tuple (…

      4d21297
    24. serhiy-storchaka
      closed this as completedon Jan 6, 2026
    25. thunder-coding
      added a commit that references this issue on Feb 15, 2026

      pythongh-41779: Allow defining any __slots__ for a class derived from…

      ed979b6
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    • arigo

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.