On 04/09/2019 23:25:32, r...@hwyl.org wrote:
Hi list
I have a proposal for a minor (improvement?) to dict declarations.
I find a common requirement I have is to include one or more entries in a dict
only if some condition is met. Currently, the usual way of doing that is to add
one or more if statements.
Simple (but hopefully relatable-to-real-world-code) example. Here we're
constructing a query for a database based on the arguments passed in. In some
cases we don't want to include some fields in the search at all depending on
what the values passed in are.
###
def construct_query(user_id,
max_results=10000,
active=None,
deleted=None):
query = {
'user_id': user_id
}
if max_results > 0:
query['max_results'] = max_results
if active is not None:
query['active'] = active
if deleted is not None:
query['deleted'] = deleted
return query
###
A chance for me to bang the drum on one of my pet themes:
Sometimes the readability of code is improved by breaking the sacred
taboo of 1 statement per line, if it allows similar constructs to be
vertically aligned:
if max_results > 0 : query['max_results'] = max_results
if active is not None : query['active'] = active
if deleted is not None: query['deleted'] = deleted
If this comes out ragged in your browser, the colons and equal signs are meant
to be vertically aligned.
Rob Cliffe
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/python-ideas@python.org/message/XWAAERJD5WIJXZLDHWCOLOIUW3UXT3SG/
Code of Conduct: http://python.org/psf/codeofconduct/