Skip to content

Instantly share code, notes, and snippets.

@mscuthbert
Last active August 29, 2015 14:27
Show Gist options
  • Save mscuthbert/b5d94a78f8559740a270 to your computer and use it in GitHub Desktop.
Save mscuthbert/b5d94a78f8559740a270 to your computer and use it in GitHub Desktop.
class X(object):
__slots__ = ('hi',)
class Y(X):
__slots__ = ('bye',)
x = X()
print(x.__slots__)
# ('hi',)
y = Y()
print(y.__slots__)
# ('bye',)
y.bye = 'yep' # of course this works.
y.hi = 'yep' # this works too even though 'hi' is not in y.__slots__
y.seeya = 'ERROR HERE!!' # slots are working.
class Z(X):
pass # no slots
z = Z()
print(z.__slots__)
# ('hi',)
z.blah = 5 # works great
# z has slots and a dict.
print(z.__dict__)
{'blah': 5}
# in defining __getstate__ on X, if you want X to be inherited properly,
# it needs to search in the .__class__.mro() for all __slots__, and
# also check to see if a __dict__ exists.
@lebigot
Copy link

lebigot commented Aug 22, 2015

Line 12 shows that slots are actually inherited. I guess that you meant that __slots__ is not inherited.

@mscuthbert
Copy link
Author

Thanks! There was some ambiguity. Updated.

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