Collection of lower-level utilities that enhance code compatibility and validation.
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: base 'Bar' does not define __slots__
Slotted behavior can also be achieved by using the SlottedMeta metaclass.
>>> from six import with_metaclass
>>> from slotted import SlottedMeta
>>> class Foo(with_metaclass(SlottedMeta, object)):
... 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-4.0.0.tar.gz
(8.4 kB
view hashes)
Built Distribution
Close
Hashes for slotted-4.0.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c270dfe59a1dca8720f46b21183af3024a5d2afaef42535850421f42594405f4 |
|
MD5 | 4001b4bfbdd15767e8e626b2dd29adf5 |
|
BLAKE2b-256 | 6e8880d4e08a64793dae5b043f09c70c5509e45b9edfca36cba4db3687c241a8 |