Liste des Groupes | Revenir à cu programmer |
On 2 Apr 2024 08:26:14 GMT, Stefan Ram wrote: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.
ram@zedat.fu-berlin.de (Stefan Ram) wrote or quoted:It’s bloody horrible Python even when wrapped correctly. I think Python’s
>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.
version of the conditional expression is a complete abortion.
How about this, to at least get things the same way round as in the moreHow about just writing Python code the way pretty much everyone else writes Python code? In a simple and obvious manner :
usual C-style conditional expression:
def diff(x) :
return \
[
lambda :
[
lambda :
[
lambda : None,
lambda : ['sum', diff(x[1]), diff(x[2])],
][x[0] == 'sum'](),
lambda : 0,
][isinstance(x, str)](),
lambda : 1,
][x == 'x']()
#end diff
Les messages affichés proviennent d'usenet.