<div dir="ltr">Ideally, the "next_depth" field would also not be defined, which may be easier to handle with the later syntax of putting an 'if' out in front.<br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Apr 26, 2019 at 11:56 AM Sebastian Kreft <<a href="mailto:skreft@gmail.com">skreft@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Apr 26, 2019 at 11:07 AM Joshua Marshall <<a href="mailto:j.marshall@arroyo.io" target="_blank">j.marshall@arroyo.io</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div>Hello all,</div><div><br></div><div>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:</div><div><br></div><div>```</div><div>def my_func(val_1, val_2):</div><div>    return {</div><div>        "field_1": val_1,</div><div>        "next_depth": {</div><div>            "field_2": val_2<br></div><div>        }<br></div><div>    }<br></div><div>```</div><div><br></div><div>What I want to do is:</div><div>```<div>def my_func(val_1, val_2):</div><div>    return {</div><div>        "field_1": val_1 if val_1,</div><div>        "next_depth": {</div><div>            "field_2": val_2 if val_2<br></div><div>        }<br></div><div>    }<br></div><div>```</div></div></div></div></div></blockquote><div>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). <br> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div><div><div><br></div><div>Or:</div><div>```<div>def my_func(val_1, val_2):</div><div>    return {</div><div>        if val_1 : "field_1": val_1,</div><div>        "next_depth": {</div><div>            if val_2: "field_2": val_2<br></div><div>        }<br></div><div>    }<br></div><div>```</div></div></div></div><div><br></div><div>Where each conditional in this context functions as:</div><div>```<br></div><div>if value:<br>    d["your_key"] = value<br></div><div>```</div><div>for each conditionally added and set key.<br></div><div><br></div><div>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`.</div><div><br></div><div>Thanks to Tom Forbes and Jim Kelly for helping critique the idea thus far.<br></div></div></div></div>

<br>
<div style="font-size:1.3em"><img src="https://res.cloudinary.com/arroyo/image/upload/c_scale,w_275/v1519146345/logos/arroyo/arroyo_no_tagline.png"></div><div style="font-size:1.3em"><br></div><div><font size="1" face="Arial, Helvetica, sans-serif">Please be advised that this emai</font><font size="1" face="Arial, Helvetica, sans-serif">l 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.</font></div><div style="font-size:1.3em"><font size="1" face="Arial, Helvetica, sans-serif"><br></font></div><div style="font-size:1.3em;text-align:center"><a href="https://twitter.com/arroyo_networks" target="_blank"><img src="https://res.cloudinary.com/arroyo/image/upload/q_auto,c_scale,w_30/v1477443435/email/twitter-circle-badge.png" style="font-size: 1.3em;"></a>  <a href="https://www.linkedin.com/company/arroyo-networks" target="_blank"><img src="https://res.cloudinary.com/arroyo/image/upload/q_auto,c_scale,w_30/v1477443435/email/linkedin-circle-badge.png" style="font-size: 1.3em;"></a>  <a href="https://www.github.com/ArroyoNetworks" target="_blank"><img src="https://res.cloudinary.com/arroyo/image/upload/q_auto,c_scale,w_30/v1477443435/email/github-circle-badge.png"></a></div>_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail-m_4585799476456745986gmail_signature">Sebastian Kreft</div></div>
</blockquote></div>

<br>
<div style="font-size:1.3em"><img src="https://res.cloudinary.com/arroyo/image/upload/c_scale,w_275/v1519146345/logos/arroyo/arroyo_no_tagline.png"></div><div style="font-size:1.3em"><br></div><div><font face="Arial, Helvetica, sans-serif" size="1">Please be advised that this emai</font><font face="Arial, Helvetica, sans-serif" size="1">l 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.</font></div><div style="font-size:1.3em"><font face="Arial, Helvetica, sans-serif" size="1"><br></font></div><div style="font-size:1.3em;text-align:center"><a href="https://twitter.com/arroyo_networks" target="_blank"><img src="https://res.cloudinary.com/arroyo/image/upload/q_auto,c_scale,w_30/v1477443435/email/twitter-circle-badge.png" style="font-size:1.3em"></a>  <a href="https://www.linkedin.com/company/arroyo-networks" target="_blank"><img src="https://res.cloudinary.com/arroyo/image/upload/q_auto,c_scale,w_30/v1477443435/email/linkedin-circle-badge.png" style="font-size:1.3em"></a>  <a href="https://www.github.com/ArroyoNetworks" target="_blank"><img src="https://res.cloudinary.com/arroyo/image/upload/q_auto,c_scale,w_30/v1477443435/email/github-circle-badge.png"></a></div>