[Python-ideas] ternary without else

Antoine Rozo antoine.rozo at gmail.com
Sat May 26 05:24:42 EDT 2018


Dismiss my message, I have read `if "art_wt" not in article`. But in the
same way, you could have a function to reset a value in your dict if the
current value evaluates to False.

2018-05-26 11:21 GMT+02:00 Antoine Rozo <antoine.rozo at gmail.com>:

> > if not article["art_wt"]: article["art_wt"] = 0
> > if not article["px_pchs"]: article["px_pchs"] = 0
> > if not article["px_calc"]: article["px_calc"] = 0
> > if not article["px_sell"]: article["px_sell"] = 0
>
> I think what you need is the setdefault method of dictionnaries, instead
> of a new syntax construct.
>
> 2018-05-26 1:52 GMT+02:00 Carl Smith <carl.input at gmail.com>:
>
>> ​You cannot have `expression if expression` in a language that also
>> supports
>> `expression if expression else expression` (like Python).​ Otherwise, you
>> have
>> the dangling else problem:
>>
>> https://en.wikipedia.org/wiki/Dangling_else
>>
>> -- Carl Smith
>> carl.input at gmail.com
>>
>> On 25 May 2018 at 15:21, Rob Cliffe via Python-ideas <
>> python-ideas at python.org> wrote:
>>
>>>
>>>
>>> On 25/05/2018 12:38, Steven D'Aprano wrote:
>>>
>>>>
>>>> PEP 8 is not holy writ. Ignore it when your code is better for ignoring
>>>> it.
>>>>
>>>>
>>>> +1.  Not infrequently I judge that my code is easier both to read and
>>> to maintain with more than 1 (short) statement on a line,
>>> often when it allows similar items to be aligned vertically.  A
>>> spur-of-the moment, totally invented example:
>>>     if MON <= day <= FRI:    Rate = Normal
>>>     if day==SAT:                     Rate = DoubleTime
>>>     if day==SUN:                    Rate = TripleTime
>>> I also feel no inhibitions about using Jacco's original example:
>>>     if cond: do_something
>>> Rob Cliffe
>>>
>>> _______________________________________________
>>> 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/
>>>
>>
>>
>> _______________________________________________
>> 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/
>>
>>
>
>
> --
> Antoine Rozo
>



-- 
Antoine Rozo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180526/b5fc10a3/attachment-0001.html>


More information about the Python-ideas mailing list