On Sep 8, 2019, at 11:34, Chris Angelico <[email protected]> wrote:
>
> You're right that the triple repeated name is not caused by your
> style. However, your suggestion also doesn't solve it. What I'd
> probably want to do, here, is build the dictionary with everything,
> and then have a separate pass that removes anything that's None (or
> maybe "any of these keys, if the value is None"), and then just
> special-case the requirement for max_results to be positive. But I'd
> be looking to make a broader change somewhere, taking into account a
> lot more code, rather than doing this whole "take local names and
> plonk them into a dict" thing at all.
If you really want to avoid repeating names, the only plausible option seems to
be putting them in a namespace that you can then filter and dictify, instead of
storing them as a bunch of separate locals.
Well, I suppose you could use locals itself as that namespace:
query_data = {k: v for (k, v) in locals().items() if k in {'max_results',
'active', 'deleted'} and v}
… but that’s pretty horrible.
And I suppose there is something Python could do to alleviate the repetitions.
The stalled proposal for magic keyword arguments would let you write something
like this:
query_data = dict(max_results=, active=, deleted=)
… which you could then filter. But I don’t think most people liked that
proposal, and I don’t think this is a good enough use case to revive it.
_______________________________________________
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/ZZXPI4BCYPN2RICUTLHTFYV63GNBKEEY/
Code of Conduct: http://python.org/psf/codeofconduct/