Message413212
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. |
|
Date |
User |
Action |
Args |
2022-02-14 05:15:32 | med2277 | set | recipients:
+ med2277, gvanrossum, JelleZijlstra, Gobot1234, kj, AlexWaygood |
2022-02-14 05:15:32 | med2277 | set | messageid: <1644815732.91.0.0719476209699.issue46743@roundup.psfhosted.org> |
2022-02-14 05:15:32 | med2277 | link | issue46743 messages |
2022-02-14 05:15:32 | med2277 | create | |
|