I use attrs
library.
I use some attributes that are set by the __attrs_post_init__
method.
For them, I want to prevent them from being part of the constructor.
Is it ok to not put a default value, or is it an implicit requirement of putting init=False
?
The doc is not really clear about it.
import attrs
@attrs.define
class MyClass:
val1: str = attrs.field(init=False)
def __attrs_post_init__(self):
self.val1="something"
a = MyClass()
print(a)
a.val1
This code is working, however I wonder if it’s relevant regarding the best practices.
That's perfectly fine. With init=False
you're opting out of attrs's __init__
.
You can always try import inspect; inspect.getsource(MyClass.__init__)
if you're curious what's happening under the hood.