All;
def foo(i):
return 100 if i < 10 else pass
return 200 if i < 20 else pass
return 1
Why this not works in python? I suppose this code may works same as:
def foo(i):
if i < 10:
return 100
elif i < 20:
return 200
else:
return 1
Thanks!
In the documentation you will see that the "ternary operator" should be like this:
conditional_expression ::= or_test ["if" or_test "else" expression]
expression ::= conditional_expression | lambda_expr
and pass
is a statement not an expression
return 100 if i < 10 else pass
you should read it as return (100 if i < 10 else pass)
so pass
isn't a value
read your code like this:
return (100 if (i < 10) else pass)
pass is not a value you can return. The following code would work:
def foo(i):
return 100 if i < 10 else (200 if i < 20 else 1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With