On 2017-02-28 12:04, Michel Desmoulin 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.

[snip]

If None is a valid value, then use a sentinel:

MISSING = object()
val = conf.get('setting_name', MISSING)
if val is MISSING:
    val = load_from_db('setting_name')

_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to