Enforces usage of '__slots__' for python classes
Project description
Enforces usage of __slots__ for python classes and provides pickling capabilities.
Examples
When defining a Slotted class with no __slots__ declaration, it assumes it has empty slots, which is equivalent to declaring __slots__ = ().
>>> from slotted import Slotted
>>> class Foo(Slotted):
... pass # implicit declaration of __slots__ = ()
...
>>> foo = Foo()
>>> foo.bar = 1
Traceback (most recent call last):
AttributeError: 'Foo' object has no attribute 'bar'
Slotted classes can be mixed with regular classes as long as they and all of their bases implement __slots__.
>>> from slotted import Slotted
>>> class Bar:
... __slots__ = ("bar",)
>>> class Foo(Bar, Slotted):
... __slots__ = ("foo",)
...
>>> foo = Foo()
If any non-Slotted class anywhere in the chain does not implement __slots__, a TypeError exception is raised.
>>> from slotted import Slotted
>>> class Bar:
... pass
>>> class Foo(Bar, Slotted):
... __slots__ = ("foo",)
...
Traceback (most recent call last):
TypeError: <class '__main__.Bar'> does not define __slots__
Slotted behavior can also be achieved by using the SlottedMeta metaclass.
>>> from slotted import SlottedMeta
>>> class Foo(metaclass=SlottedMeta):
... pass # implicit declaration of __slots__ = ()
...
>>> foo = Foo()
>>> foo.bar = 1
Traceback (most recent call last):
AttributeError: 'Foo' object has no attribute 'bar'
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
slotted-2.1.0.tar.gz
(3.8 kB
view hashes)
Built Distribution
Close
Hashes for slotted-2.1.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 14b85f067687a79813da1bf345aea7de05fd58412f7cc2a93d4ad4749f863c2f |
|
MD5 | 8827b14a8930760b04735b993970fccd |
|
BLAKE2b-256 | 09396bdd2ccb9351c153012bf8a5efccf84f33a0ba4ecab2bd692c8322fdbf59 |