[Python-ideas] ternary without else
Philipp A.
flying-sheep at web.de
Fri May 25 06:45:00 EDT 2018
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 <storchaka at gmail.com> 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 at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180525/8f27ba0a/attachment.html>
More information about the Python-ideas
mailing list