Last active
August 29, 2015 14:27
-
-
Save mscuthbert/b5d94a78f8559740a270 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Thanks! There was some ambiguity. Updated.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 12 shows that slots are actually inherited. I guess that you meant that
__slots__
is not inherited.