Skip to content

Instantly share code, notes, and snippets.

@jd
Created October 1, 2019 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jd/072eed87899d234098ac22bdf3744798 to your computer and use it in GitHub Desktop.
Save jd/072eed87899d234098ac22bdf3744798 to your computer and use it in GitHub Desktop.
class A(object):
pass
class B(object):
__slots__ = ('foobar',)
def __init__(self):
self.x = 123
class C(A):
__slots__ = ('foobar',)
def __init__(self):
self.x = 123
>>> A()
<__main__.A object at 0x10caf4d10>
>>> C()
<__main__.C object at 0x10caf38c0>
>>> B()
Traceback (most recent call last):
File "slots.py", line 18, in <module>
B()
File "slots.py", line 8, in __init__
self.x = 123
AttributeError: 'B' object has no attribute 'x'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment