Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions docs/init.md
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,24 @@ If present, the hooks are executed in the following order:

Notably this means, that you can access all attributes from within your validators, but your converters have to deal with invalid values and have to return a valid value.

The generated `__init__` initializes fields directly, so it skips `on_setattr` hooks.
Those hooks run on assignments such as `obj.x = value`.
Use a converter to transform a value during initialization or a validator to check it.

```{doctest}
>>> def double_on_assignment(instance, attribute, value):
... return value * 2
>>> @define
... class Counter:
... count: int = field(on_setattr=double_on_assignment)
>>> counter = Counter(3)
>>> counter.count
3
>>> counter.count = 3
>>> counter.count
6
```


## Derived Attributes

Expand Down