| 1 |
# Glossary |
| 2 |
|
| 3 |
:::{glossary} |
| 4 |
dunder methods |
| 5 |
"Dunder" is a contraction of "double underscore". |
| 6 |
|
| 7 |
It's methods like `__init__` or `__eq__` that are sometimes also called *magic methods* or it's said that they implement an *object protocol*. |
| 8 |
|
| 9 |
In spoken form, you'd call `__init__` just "dunder init". |
| 10 |
|
| 11 |
Its first documented use is a [mailing list posting](https://mail.python.org/pipermail/python-list/2002-September/155836.html) by Mark Jackson from 2002. |
| 12 |
|
| 13 |
dict classes |
| 14 |
A regular class whose attributes are stored in the {attr}`object.__dict__` attribute of every single instance. |
| 15 |
This is quite wasteful especially for objects with very few data attributes and the space consumption can become significant when creating large numbers of instances. |
| 16 |
|
| 17 |
This is the type of class you get by default both with and without *attrs* (except with the next APIs {func}`attrs.define()`, [`attrs.mutable()`](attrs.mutable), and [`attrs.frozen()`](attrs.frozen)). |
| 18 |
|
| 19 |
slotted classes |
| 20 |
A class whose instances have no {attr}`object.__dict__` attribute and [define](https://docs.python.org/3/reference/datamodel.html#slots) their attributes in a `object.__slots__` attribute instead. |
| 21 |
In *attrs*, they are created by passing `slots=True` to `@attr.s` (and are on by default in {func}`attrs.define()`, [`attrs.mutable()`](attrs.mutable), and [`attrs.frozen()`](attrs.frozen)). |
| 22 |
|
| 23 |
Their main advantage is that they use less memory on CPython[^pypy] and are slightly faster. |
| 24 |
|
| 25 |
However, they also come with several possibly surprising gotchas: |
| 26 |
|
| 27 |
- Slotted classes don't allow for any other attribute to be set except for those defined in one of the class' hierarchies `__slots__`: |
| 28 |
|
| 29 |
```{doctest} |
| 30 |
>>> from attr import define |
| 31 |
>>> @define |
| 32 |
... class Coordinates: |
| 33 |
... x: int |
| 34 |
... y: int |
| 35 |
... |
| 36 |
>>> c = Coordinates(x=1, y=2) |
| 37 |
>>> c.z = 3 |
| 38 |
Traceback (most recent call last): |
| 39 |
... |
| 40 |
AttributeError: 'Coordinates' object has no attribute 'z' |
| 41 |
``` |
| 42 |
|
| 43 |
- Slotted classes can inherit from other classes just like non-slotted classes, but some of the benefits of slotted classes are lost if you do that. |
| 44 |
If you must inherit from other classes, try to inherit only from other slotted classes. |
| 45 |
|
| 46 |
- However, [it's not possible](https://docs.python.org/3/reference/datamodel.html#slots) to inherit from more than one class that has attributes in `__slots__` (you will get an `TypeError: multiple bases have instance lay-out conflict`). |
| 47 |
|
| 48 |
- It's not possible to monkeypatch methods on slotted classes. |
| 49 |
This can feel limiting in test code, however the need to monkeypatch your own classes is usually a design smell. |
| 50 |
|
| 51 |
If you really need to monkeypatch an instance in your tests, but don't want to give up on the advantages of slotted classes in production code, you can always subclass a slotted class as a dict class with no further changes and all the limitations go away: |
| 52 |
|
| 53 |
```{doctest} |
| 54 |
>>> import unittest.mock |
| 55 |
>>> @define |
| 56 |
... class Slotted: |
| 57 |
... x: int |
| 58 |
... |
| 59 |
... def method(self): |
| 60 |
... return self.x |
| 61 |
>>> s = Slotted(42) |
| 62 |
>>> s.method() |
| 63 |
42 |
| 64 |
>>> with unittest.mock.patch.object(s, "method", return_value=23): |
| 65 |
... pass |
| 66 |
Traceback (most recent call last): |
| 67 |
... |
| 68 |
AttributeError: 'Slotted' object attribute 'method' is read-only |
| 69 |
>>> @define(slots=False) |
| 70 |
... class Dicted(Slotted): |
| 71 |
... pass |
| 72 |
>>> d = Dicted(42) |
| 73 |
>>> d.method() |
| 74 |
42 |
| 75 |
>>> with unittest.mock.patch.object(d, "method", return_value=23): |
| 76 |
... assert 23 == d.method() |
| 77 |
``` |
| 78 |
|
| 79 |
- Slotted classes must implement {meth}`__getstate__ <object.__getstate__>` and {meth}`__setstate__ <object.__setstate__>` to be serializable with {mod}`pickle` protocol 0 and 1. |
| 80 |
Therefore, *attrs* creates these methods automatically for slotted classes. |
| 81 |
|
| 82 |
:::{note} |
| 83 |
When decorating with `@attr.s(slots=True)` and the class already implements the {meth}`__getstate__ <object.__getstate__>` and {meth}`__setstate__ <object.__setstate__>` methods, they will be *overwritten* by *attrs* autogenerated implementation by default. |
| 84 |
|
| 85 |
This can be avoided by setting `@attr.s(getstate_setstate=False)` or by setting `@attr.s(auto_detect=True)`. |
| 86 |
|
| 87 |
{func}`~attrs.define` sets `auto_detect=True` by default. |
| 88 |
::: |
| 89 |
|
| 90 |
Also, [think twice](https://www.youtube.com/watch?v=7KnfGDajDQw) before using {mod}`pickle`. |
| 91 |
|
| 92 |
- Slotted classes are weak-referenceable by default. |
| 93 |
This can be disabled in CPython by passing `weakref_slot=False` to `@attr.s` [^pypyweakref]. |
| 94 |
|
| 95 |
- Since it's currently impossible to make a class slotted after it's been created, *attrs* has to replace your class with a new one. |
| 96 |
While it tries to do that as graciously as possible, certain metaclass features like {meth}`object.__init_subclass__` do not work with slotted classes. |
| 97 |
|
| 98 |
- The {attr}`class.__subclasses__` attribute needs a garbage collection run (which can be manually triggered using {func}`gc.collect`), for the original class to be removed. |
| 99 |
See issue [#407](https://github.com/python-attrs/attrs/issues/407) for more details. |
| 100 |
|
| 101 |
- Pickling of slotted classes will fail if you define a class with missing attributes. |
| 102 |
|
| 103 |
This situation can occur if you define an `attrs.field(init=False)` and don't set the attribute by hand before pickling. |
| 104 |
::: |
| 105 |
|
| 106 |
[^pypy]: On PyPy, there is no memory advantage in using slotted classes. |
| 107 |
|
| 108 |
[^pypyweakref]: On PyPy, slotted classes are naturally weak-referenceable so `weakref_slot=False` has no effect. |