Mehdi2277 <[email protected]> added the comment:
Hmm, I actually have code that does something very similar to that. But it does
not set it in `__init__` and instead uses it in a property where it is
available. Tweaking your code,
```py
class DefaultBox(Generic[T]):
def __init__(self, value: T | None = None):
self._default_value = value
self._value_set = False
@property
def value(self) -> T:
if not self._value_set:
self._value = self._default_value if self._default_value is not None
else self.__orig_class__.__args__[0]()
return self._value
```
I think should work fine. Any reason initializing value afterwards is an issue?
Or is the main goal that the original code is simpler then this lazy
initialization workaround.
----------
nosy: +med2277
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue46743>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com