Liste des Groupes | Revenir à cu shell |
David Brown <david.brown@hesbynett.no> writes:I am not very familiar with Perl, and don't know what are expressions or statements. Perhaps I have been imagining things. I had the idea that in Perl you could write "<do_this> if <condition>" as an alternative to the more common imperative language ordering "if <condition> then <do_this>".On 03/04/2024 02:23, Lawrence D'Oliveiro wrote:No, Perl's conditional expressions use the same syntax as C's.On 2 Apr 2024 08:26:14 GMT, Stefan Ram wrote:>
>ram@zedat.fu-berlin.de (Stefan Ram) wrote or quoted:It’s bloody horrible Python even when wrapped correctly. I think
>def diff( x ):>
return 1 if x == 'x' else 0 if type( x )is str else[ 'sum', diff( x[
1 ]), diff( x[ 2 ])]
if x[ 0 ]== 'sum' else None
Oops! That was one long line starting with "return";
it was wrapped by the newsreader, but is not correct Python anymore
when wrapped this way.
Python’s version of the conditional expression is a complete
abortion.
That's probably the reason almost no one uses it. That post is the
first time I have ever seen conditional expressions outside of a brief
mention in a tutorial on Python conditionals showing how to write
normal conditionals in the language. I think Python stole this one
from Perl.
As for whether Python's conditional expression syntax, it's not clearI think expr1 and expr2 belong naturally together, as you are selecting one or the other. If you are using a short-circuit evaluation, you would express it in words as "evaluate cond, and based on that, evaluate either expr1 and expr2". Having "expr1" first is a bit like a recipe that says "add the sugar to the egg whites, having first beaten the egg whites". It is an ordering that does not suit well in an imperative language (I know Python is multi-paradigm, but it is basically imperative).
that (cond ? expr1 : expr2) is better or worse than (expr1 if cond else
expr2) (unless you happen to be familiar with one of them).
Les messages affichés proviennent d'usenet.