[Python-ideas] Syntax to conditionally define a field in a dict

Sebastian Kreft skreft at gmail.com
Fri Apr 26 11:56:16 EDT 2019


On Fri, Apr 26, 2019 at 11:07 AM Joshua Marshall <j.marshall at arroyo.io>
wrote:

> Hello all,
>
> I have a use case where I need to send a `dict` to a module as an
> argument.  Inside of this, it has a multi-level structure, but each field I
> need to set may only be set to a single value.  Fields must be valid,
> non-empty strings.  It looks a lot like the following in my code:
>
> ```
> def my_func(val_1, val_2):
>     return {
>         "field_1": val_1,
>         "next_depth": {
>             "field_2": val_2
>         }
>     }
> ```
>
> What I want to do is:
> ```
> def my_func(val_1, val_2):
>     return {
>         "field_1": val_1 if val_1,
>         "next_depth": {
>             "field_2": val_2 if val_2
>         }
>     }
> ```
>
If val_2 here evaluates to falsey, will next_depth still be defined? From
the code I would say that no. But your use case may require to not define
the next_depth subdict without any values, as that may break the receiver
expectations (think of JSON Schema).


>
> Or:
> ```
> def my_func(val_1, val_2):
>     return {
>         if val_1 : "field_1": val_1,
>         "next_depth": {
>             if val_2: "field_2": val_2
>         }
>     }
> ```
>
> Where each conditional in this context functions as:
> ```
> if value:
>     d["your_key"] = value
> ```
> for each conditionally added and set key.
>
> From the slack channel #learning_python, there are a number of more
> general points which need to be handled.  The more core syntax, which
> should be valid throughout the language, would be to have statements like
> `x = y if cond` and `x[y if cond]`.  The first of these intuitively
> reorganizes to `if cond: x = y`, but the second is not as clear, with a
> likely equivalent of `if cond: x[y] else raise Exception`.
>
> Thanks to Tom Forbes and Jim Kelly for helping critique the idea thus far.
>
>
> Please be advised that this email may contain confidential information.
> If you are not the intended recipient, please notify us by email by
> replying to the sender and delete this message. The sender disclaims that
> the content of this email constitutes an offer to enter into, or the
> acceptance of, any agreement; provided that the foregoing does not
> invalidate the binding effect of any digital or other electronic
> reproduction of a manual signature that is included in any attachment.
>
> <https://twitter.com/arroyo_networks>
> <https://www.linkedin.com/company/arroyo-networks>
> <https://www.github.com/ArroyoNetworks>
> _______________________________________________
> 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/
>


-- 
Sebastian Kreft
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190426/1e341abd/attachment.html>


More information about the Python-ideas mailing list