Skip to main content

Slotted

https://github.com/brunonicko/slotted/workflows/MyPy/badge.svg https://github.com/brunonicko/slotted/workflows/Lint/badge.svg https://github.com/brunonicko/slotted/workflows/Tests/badge.svg https://readthedocs.org/projects/slotted/badge/?version=stable https://img.shields.io/github/license/brunonicko/slotted?color=light-green https://static.pepy.tech/personalized-badge/slotted?period=total&units=international_system&left_color=grey&right_color=brightgreen&left_text=Downloads https://img.shields.io/pypi/pyversions/slotted?color=light-green&style=flat

Overview

Enforces usage of __slots__ for Python classes.

Motivation

Besides the performance benefits, using __slots__ also prevents the client code from setting attributes that were not initially defined for instances of classes, which usually happens by mistake especially in environments where static type checking is not being performed.

So forcing it upon a class and its subclasses might be a desirable thing to do for classes that are part of an API, for example.

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(object):
...     __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(object):
...     pass
>>> class Foo(Bar, Slotted):
...     __slots__ = ("foo",)
...
Traceback (most recent call last):
TypeError: base 'Bar' is not slotted

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'

abc

slotted also provides generic versions of the collection.abc classes.

>>> from typing import TypeVar
>>> from slotted import SlottedMapping, SlottedSequence, SlottedSet
>>> KT = TypeVar("KT")
>>> VT = TypeVar("VT")
>>> class MyMapping(SlottedMapping[KT, VT]):
...     pass # implicit declaration of __slots__ = ()
...
>>> class MySequence(SlottedSequence[VT]):
...     pass # implicit declaration of __slots__ = ()
...
>>> class MySet(SlottedSet[VT]):
...     pass # implicit declaration of __slots__ = ()
...

For Python 2.7, slotted adds a SlottedCollection class, even though the original Collection is not available.

>>> from typing import TypeVar
>>> from slotted import SlottedCollection
>>> T = TypeVar("T")
>>> class MyCollection(SlottedCollection[T]):
...     pass # implicit declaration of __slots__ = ()
...

Metadata

Release files for slotted 5.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for slotted 5.1.0
File Size Uploaded
slotted-5.1.0.tar.gz 14.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for slotted 5.1.0
File Interpreter ABI Platform
slotted-5.1.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 21.9 kB

Release files / slotted-5.1.0.tar.gz

Download URL slotted-5.1.0.tar.gz
Size 14.3 kB
Tags Source
SHA-256 checksum
How to use checksums
817fddb559808f7e17a9b6fa6a86667a9477736797fd7bbe0040e995275aaec6
BLAKE2b-256 checksum
How to use checksums
4f4d113f0478c1394e10fda371a375dccf83257370320c0b15d11908ef39bdb7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.2

Release files / slotted-5.1.0-py2.py3-none-any.whl

Download URL slotted-5.1.0-py2.py3-none-any.whl
Size 7.5 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
aa242d048c1d766a1f05e4d432f9c45847718f0e016134556935337757e70b37
BLAKE2b-256 checksum
How to use checksums
77301fc27f2c85f2e0d7505312507547a539834ac1c24c02480f3e1b97c34be5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.2

Release history Release notifications | RSS feed

This release

5.1.0 This release

2 release files

5.0.1

2 release files

5.0.0

2 release files

4.5.0

2 release files

4.4.0

2 release files

4.3.0

2 release files

4.2.1

2 release files

4.2.0

2 release files

4.1.0

2 release files

4.0.0

2 release files

3.1.1

2 release files

3.1.0

2 release files

3.0.0

2 release files

2.1.0

2 release files

2.0.0

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page