Skip to content

Commit

Permalink
docs: wordsmith
Browse files Browse the repository at this point in the history
  • Loading branch information
hynek authored Dec 31, 2023
1 parent 341839a commit bfbea3a
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions docs/how-does-it-work.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,14 @@ Depending on whether a class is a dict class or a slotted class, *attrs* uses a

Once constructed, frozen instances don't differ in any way from regular ones except that you cannot change its attributes.


### Dict Classes

Dict classes -- i.e. regular classes -- simply assign the value directly into the class' eponymous `__dict__` (and there's nothing we can do to stop the user to do the same).

The performance impact is negligible.


### Slotted Classes

Slotted classes are more complicated.
Expand Down Expand Up @@ -102,17 +104,18 @@ Frozen dict classes have barely a performance impact, unfrozen slotted classes a

## Cached Properties on Slotted Classes.

By default, the standard library `functools.cached_property` decorator does not work on slotted classes,
because it requires a `__dict__` to store the cached value.
This could be surprising when uses *attrs*, as makes using slotted classes so easy,
so attrs will convert `functools.cached_property` decorated methods, when constructing slotted classes.
By default, the standard library `functools.cached_property` decorator does not work on slotted classes, because it requires a `__dict__` to store the cached value.
This could be surprising when using *attrs*, as slotted classes are the default.
Therefore, *attrs* converts `functools.cached_property` decorated methods when constructing slotted classes.

Getting this working is achieved by:

* Adding names to `__slots__` for the wrapped methods.
* Adding a `__getattr__` method to set values on the wrapped methods.

For most users this should mean that it works transparently.
For most users, this should mean that it works transparently.

Note that the implementation does not guarantee that the wrapped method is called
only once in multi-threaded usage. This matches the implementation of `cached_property`
in python v3.12.
:::{note}
The implementation does not guarantee that the wrapped method is called only once in multi-threaded usage.
This matches the implementation of `cached_property` in Python 3.12.
:::

0 comments on commit bfbea3a

Please sign in to comment.