While the implementation is great, including locking to ensure the underlying function is called only once, this only works for properties rather than methods defined in modules (and more generally, any method).
> On 26 Apr 2020, at 20:27, Matthew Einhorn <[email protected]> wrote: > > On Sun, Apr 26, 2020 at 1:54 PM Tom Forbes <[email protected] > <mailto:[email protected]>> wrote: > This is a good idea but some cases need to be lazily evaluated. Without that > property `once()` loses a lot of utility. In the case of Django some of the > decorated functions create objects that cannot be instantiated until the > Django settings have been loaded, which rules out calling them from the > module scope. > > I believe this use case of lazily initialising objects that cannot be created > in the module scope is encountered by other projects. One example off the top > of my head might be a database connection, or some other stateful client, > that you want a single instance of but want to actually create at some point > in the future. > > I'm surprised no one mentioned cached_property > (https://docs.python.org/dev/library/functools.html#functools.cached_property) > > <https://docs.python.org/dev/library/functools.html#functools.cached_property)>!?
signature.asc
Description: Message signed with OpenPGP
_______________________________________________ Python-ideas mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/[email protected]/message/ZBTHOPGMTQOJFVGEN5CGWEGVEFQCGPFG/ Code of Conduct: http://python.org/psf/codeofconduct/
