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

2019-04-26 Thread Christopher Barker
Others have responded, but a note: > 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 } } ``` I am finding this very confusing as to how to generalize this: How do we kn

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

2019-04-26 Thread MRAB
On 2019-04-26 16:56, Sebastian Kreft wrote: On Fri, Apr 26, 2019 at 11:07 AM Joshua Marshall > 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 f

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

2019-04-26 Thread Joshua Marshall
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. On Fri, Apr 26, 2019 at 11:56 AM Sebastian Kreft wrote: > > On Fri, Apr 26, 2019 at 11:07 AM Joshua Marshall > wrote: > >> Hello all, >> >> I have a us

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

2019-04-26 Thread Ned Batchelder
On 4/26/19 11:03 AM, Joshua Marshall 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 loo

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

2019-04-26 Thread Sebastian Kreft
On Fri, Apr 26, 2019 at 11:07 AM Joshua Marshall 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-em

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

2019-04-26 Thread Jonathan Fine
Hi Joshua Sounds to me that you want a solution soon, rather than in a future version of Python. Perhaps this works for you. def prune_nones(d): for k, v in list(d.items()): if v is None: del d[k] if type(v) is dict: prune_nones(v) >>> d = dict(a=1, b=

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

2019-04-26 Thread Joshua Marshall
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

Re: [Python-ideas] Proposal: "?" Documentation Operator and easy reference to argument types/defaults/docstrings

2019-04-26 Thread Stephen J. Turnbull
David Mertz writes: > Why not just this in existing Python: > > def func_1( If I understand the OP's POV, one reason is that the following comments are not available to help() in existing Python. (From the Picky-Picky-Picky Dept: It's a syntax error since a and b are not separated by the