Movatterモバイル変換


[0]ホーム

URL:


[Python-ideas] Another use case for the 'lazy' (aka 'delayed') keyword

Mark E. Haasemehaase at gmail.com
Tue Feb 28 10:41:53 EST 2017


There are plenty of PEP-505 threads in the archives, plus the PEP itself. Iwas only pointing out that the use case in thread might also be solved withan existing proposal*, so I won't derail this thread any further.*There are actually several proposals for short-circuit evaluation,including PEP-531 and PEP-532.On Tue, Feb 28, 2017 at 10:29 AM, Joseph Hackman <josephhackman at gmail.com>wrote:> I like null coalesce too. :)>> I know that BDFL has said no to ? Before, but was that for the if-then> shorthand only?>> Perhaps submit a new thread to this list so people can discuss/find?>> -Joseph>> On Feb 28, 2017, at 10:21 AM, Mark E. Haase <mehaase at gmail.com> wrote:>> This could be solved with a null-coalescing operator, e.g. PEP-505.>>    >>> val = conf.get('setting_name') ?? load_from_db('setting_name')>> The right side isn't evaluated unless the left side is None. It's similar> to this:>>    >>> val = conf.get('setting_name') or load_from_db('setting_name')>> Except that using "or" would result in any false-y value (0, "", [], etc.)> being overridden by the result of `load_from_db(...)`.>> I'm not strongly opposed to "lazy", but I think null-coalescing is easier> to reason about.>>> On Tue, Feb 28, 2017 at 7:04 AM, Michel Desmoulin <>desmoulinmichel at gmail.com> wrote:>>> The debate on the 'lazy' keyword seems to have settled, but I don't know>> if somebody is trying to write a PEP about it.>>>> Anyway, I was doing something like this the other day:>>>> conf.get('setting_name', load_from_db('setting_name'))>>>> And then realized I could save a query not doing the load_from_db() call>> most of the time.>>>> But dict.get didn't accept callable so I couldn't do:>>>> conf.get('setting_name', lambda key: load_from_db('setting_name'))>>>> Which is standard practice in a lot of popular Python libs on Pypi.>>>> Instead I did:>>>> val = conf.get('setting_name')>> if val is None:>>     val = load_from_db('setting_name')>>>> Which is way more verbose. It also has a bug if None is a valid>> configuration value or if I expect my code to be thread safe.>>>> It was not a problem for me, but in that case one would even have to do:>>>> try:>>     val = conf['setting_name']>> except KeyError:>>     val = load_from_db('setting_name')>>>> Which is even more verbose.>>>> I was going to suggest to python-ideas to update the dict.get()>> signature to accept a callable, but it would break compatibility with>> many code actually getting a callable.>>>> We could do it for Python 4, but it's far way.>>>> Instead, I think it's a good example of were 'lazy' could help. You>> can't get simpler than:>>>> conf.get('setting_name', lazy load_from_db('setting_name'))>>>>>> _______________________________________________>> Python-ideas mailing list>>Python-ideas at python.org>>https://mail.python.org/mailman/listinfo/python-ideas>> Code of Conduct:http://python.org/psf/codeofconduct/>>>> _______________________________________________> Python-ideas mailing list>Python-ideas at python.org>https://mail.python.org/mailman/listinfo/python-ideas> Code of Conduct:http://python.org/psf/codeofconduct/>>-------------- next part --------------An HTML attachment was scrubbed...URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170228/e1562e62/attachment.html>


More information about the Python-ideasmailing list

[8]ページ先頭

©2009-2026 Movatter.jp