Re: [Python-ideas] ternary without else

2018-05-28 Thread Jacco van Dorp
2018-05-26 11:24 GMT+02:00 Antoine Rozo :
> 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.

That won't work, since at other places, I do the same with bools and
strings in the same dict. (I read out an online database, which only
yields data in json format. I dont want an extra parsing step to
transform it to another data structure than dicts. ). Also, empty
fields are set to None, no matter actual field type(actually, it
doesn't pass empty fields for some godawful reason, so I have a
__missing__ that returns None if and only if it's in a list of
expected fields. ).

As for the dangling else problem, noting a good rule (left to right ?)
and a few parens in other cases can't fix.

That said, because of the rest of this thread, it seems to me like a
statement would be the proper implementation. So the thread title has
become misleading. This would remove the dangling else problem unless
there's an if/else in the condition, which I can't figure out why
you'd need that.
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] ternary without else

2018-05-26 Thread Antoine Rozo
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 :

> > 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 :
>
>> ​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.in...@gmail.com
>>
>> On 25 May 2018 at 15:21, Rob Cliffe via Python-ideas <
>> python-ideas@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@python.org
>>> https://mail.python.org/mailman/listinfo/python-ideas
>>> Code of Conduct: http://python.org/psf/codeofconduct/
>>>
>>
>>
>> ___
>> Python-ideas mailing list
>> Python-ideas@python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
>>
>
>
> --
> Antoine Rozo
>



-- 
Antoine Rozo
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] ternary without else

2018-05-26 Thread Antoine Rozo
> 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 :

> ​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.in...@gmail.com
>
> On 25 May 2018 at 15:21, Rob Cliffe via Python-ideas <
> python-ideas@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@python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
>
>
> ___
> Python-ideas mailing list
> Python-ideas@python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
>


-- 
Antoine Rozo
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] ternary without else

2018-05-25 Thread Carl Smith
​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.in...@gmail.com

On 25 May 2018 at 15:21, Rob Cliffe via Python-ideas <
python-ideas@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@python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] ternary without else

2018-05-25 Thread Rob Cliffe via Python-ideas



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@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] ternary without else

2018-05-25 Thread Jacco van Dorp
2018-05-25 14:57 GMT+02:00 Elazar :
> The title is misleading - this has nothing to do with the conditional
> operator, except small syntactic similarity.

On second look, yeah, you're right.
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] ternary without else

2018-05-25 Thread Steven D'Aprano
On Fri, May 25, 2018 at 12:06:42PM +0200, Jacco van Dorp wrote:
> I would like to carefully suggest a half form of the ternary expression.
[...]
> However, this isn't PEP8 compliant

PEP 8 is not holy writ. Ignore it when your code is better for ignoring 
it.


> I would very much like to write:
> 
> >>> do_something if cond
> 
> and be done with it. Like a ternary expression but without the else clause.

I believe there are a number of languages out there which have clauses 
like this. I'm not going to do a language survey, but two popular 
choices are:

something if condition

something unless condition

and similar variations. For example, Ruby:

http://www.railstips.org/blog/archives/2008/12/01/unless-the-abused-ruby-conditional/



> ===Example Usecases===
> (inside loop)
> >>> continue if x > 50

That's not going to work if this is an *expression*. You can't say:

continue if x > 50 else "don't continue"

The problem with an expression is, what does this return if the clause 
is false? So the comparison to the ternary if expression is invalid. 
This would have to be a statement.



> 
> (logging)
> >>> logger.info(f"{var}") if DEBUG
> (Logging module ofc already does this, and in a much better way.
> However, if var is expensive to get it could save a bit of time.)

If the logging module already solves this problem in "a much better 
way", why are you including a much worse way?


> (singleton creation)
> >>> cls.inst = super().__new__(cls, *args, **kwargs) if cls.inst is None

And this is a problem: that is ambiguous with the if...else ternary 
expression. Presumably you want that to look like a statement:

cls.inst = super().__new__(cls, *args, **kwargs)

guarded by an if statement:

if cls.inst is None

but it can also be interpreted as an assignment:

cls.inst = 

with an incomplete ternary expression:

super().__new__(cls, *args, **kwargs) if cls.inst is None
else what

Even if we decide to make it a rule that one interpretation beats the 
other, we still have to expect people will be confused by it. So we 
ought to be cautious about syntax which is ambiguous in this way.

> (cache)
> >>> cache.warm() if cache is None

There are about a zillion things that you can write like this:

if condition:
do something


so the use-cases certainly exist. The only question is a matter of taste 
and familiarity, whether it nicer to write it as above or like:

do something if condition


-- 
Steve
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] ternary without else

2018-05-25 Thread Elazar
The title is misleading - this has nothing to do with the conditional
operator, except small syntactic similarity.

Elazar

בתאריך יום ו׳, 25 במאי 2018, 05:40, מאת Jacco van Dorp ‏<
j.van.d...@deonet.nl>:

> 2018-05-25 14:26 GMT+02:00 Kirill Balunov :
> > If it is an expression, what should `do_something if cond` return on
> > failure? If you don't care you can already use `cond and do_something`.
>
> Duh, forgot to mention.
> I wouldn't have it return anything. Ternary returns something because
> you have two options and picks one. This is conditional execution of a
> statement. I guess it would be a statement, like normal if.
>
> I guess cond and do_something would be equivalent, but that's not
> really the intention of and/or, no matter how much it's used for this
> sort of thing.
>
> Disclaimer:
> I don't really expect this to be accepted. It's more of a minor gripe
> i've occasionally had. This afternoon, I wrote:
>
> 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
>
> while preparing a dict for import in accountview. I try not to make a
> habit of dismissing syntax warnings. I consider the code above
> abundantly clear (no, I dont know what those keys mean. Since I have
> no power over that, I dont consider it detrimental to clarity.)
> ___
> Python-ideas mailing list
> Python-ideas@python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] ternary without else

2018-05-25 Thread Jacco van Dorp
2018-05-25 14:26 GMT+02:00 Kirill Balunov :
> If it is an expression, what should `do_something if cond` return on
> failure? If you don't care you can already use `cond and do_something`.

Duh, forgot to mention.
I wouldn't have it return anything. Ternary returns something because
you have two options and picks one. This is conditional execution of a
statement. I guess it would be a statement, like normal if.

I guess cond and do_something would be equivalent, but that's not
really the intention of and/or, no matter how much it's used for this
sort of thing.

Disclaimer:
I don't really expect this to be accepted. It's more of a minor gripe
i've occasionally had. This afternoon, I wrote:

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

while preparing a dict for import in accountview. I try not to make a
habit of dismissing syntax warnings. I consider the code above
abundantly clear (no, I dont know what those keys mean. Since I have
no power over that, I dont consider it detrimental to clarity.)
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] ternary without else

2018-05-25 Thread Kirill Balunov
2018-05-25 13:06 GMT+03:00 Jacco van Dorp :

> [...]
>
> I would very much like to write:
>
> >>> do_something if cond
>
> and be done with it. Like a ternary expression but without the else clause.
>
>
If it is an expression, what should `do_something if cond` return on
failure? If you don't care you can already use `cond and do_something`.

With kind regards,
-gdg
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] ternary without else

2018-05-25 Thread Philipp A.
in jinja, you can do “{{ 'foo' if bar }}”.

it evaluates to “'foo'” or an empty string (differently to python’s
formatting, “None” expands to an empty string in jinja)

similarly I often do “thing = 'foo' if bar else None” and it would be nice
if i could shorten that by making “else None” implicit.

idk how often other people do that though

Serhiy Storchaka  schrieb am Fr., 25. Mai 2018 um
12:34 Uhr:

> 25.05.18 13:06, Jacco van Dorp пише:
> > I would like to carefully suggest a half form of the ternary expression.
> >
> > Currently, you can write code like:
> >
>  if cond:
>  do_something
> >
> > However, especially if the condition and action are both really
> > simple, taking two lines feels like a bit of a waste. So I sometimes
> > write:
> >
>  if cond: do_something
> >
> > However, this isn't PEP8 compliant, and every linter complains about
> > it. They'd be right if the condition and action were a bit more
> > complicated.
> >
> > I would very much like to write:
> >
>  do_something if cond
> >
> > and be done with it. Like a ternary expression but without the else
> clause.
>
> This isn't PEP8 compliant either.
>
> I suppose that if this syntax be accepted by the compiler, it will be
> explicitly disallowed by PEP8 for the same reason as "if cond:
> do_something".
>
> It is easier to pass an option to linter that will silence this warning
> than introduce a new ambiguous syntax.
>
> For example try to interpret "[a for b if c]" if "b if c" be a valid
> expression.
>
> ___
> Python-ideas mailing list
> Python-ideas@python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] ternary without else

2018-05-25 Thread Serhiy Storchaka

25.05.18 13:06, Jacco van Dorp пише:

I would like to carefully suggest a half form of the ternary expression.

Currently, you can write code like:


if cond:
do_something


However, especially if the condition and action are both really
simple, taking two lines feels like a bit of a waste. So I sometimes
write:


if cond: do_something


However, this isn't PEP8 compliant, and every linter complains about
it. They'd be right if the condition and action were a bit more
complicated.

I would very much like to write:


do_something if cond


and be done with it. Like a ternary expression but without the else clause.


This isn't PEP8 compliant either.

I suppose that if this syntax be accepted by the compiler, it will be 
explicitly disallowed by PEP8 for the same reason as "if cond: 
do_something".


It is easier to pass an option to linter that will silence this warning 
than introduce a new ambiguous syntax.


For example try to interpret "[a for b if c]" if "b if c" be a valid 
expression.


___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/