This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author med2277
Recipients AlexWaygood, Gobot1234, JelleZijlstra, gvanrossum, kj, med2277
Date 2022-02-14.05:15:32
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1644815732.91.0.0719476209699.issue46743@roundup.psfhosted.org>
In-reply-to
Content
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.
History
Date User Action Args
2022-02-14 05:15:32med2277setrecipients: + med2277, gvanrossum, JelleZijlstra, Gobot1234, kj, AlexWaygood
2022-02-14 05:15:32med2277setmessageid: <1644815732.91.0.0719476209699.issue46743@roundup.psfhosted.org>
2022-02-14 05:15:32med2277linkissue46743 messages
2022-02-14 05:15:32med2277create