How to write the expression shorter:
return '%.0f' % 开发者_如何学Pythonfloat_var if float_var else float_var
or
if float_var:
return formatted_string
else:
return None
Thanks!
The expression <value> if <condition> else <other_value>
is pretty idiomatic already -- certainly more so than the other example, and is probably preferred whenever <value>
is simple. This is Python's ternary operator, so if you were looking for something like <condition> ? <value> : <other_value>
, that doesn't exist.
If computing <value>
or <other_value>
takes a few steps, use the longer if: ... else: ...
alternative.
I would use brackets to make the expression more readable:
return ('%.0f' % float_var) if float_var else float_var
When I first saw it I read it as
return '%.0f' % (float_var if float_var else float_var)
which would be silly. I had to try it out to make sure which way it worked.
BTW Your first example not equivalent to your second example
if float_var:
return formatted_string
else:
return None
This will always return either a formatted string or None. Your first example if you pass in anything that evaluates to False (False, 0, 0.0, "", [] etc) will return that unchanged, so your return type could be string, boolean, list, int, float etc. This is probably not what you want, especially if 0.0 is a legitimate value for float_var. I would change your code to:
return ('%.0f' % float_var) if isinstance(float_var, float) else None
alternatively:
try:
return "%.0f" % float_var
except TypeError:
return None
which will work for other integers (and longs) by converting them to float.
It is not clear what exactly you want to do.
The most literal interpretation would have it work like this
>>> float_var = 4.5 >>> '%.0f' % float_var if float_var else float_var '5' # This is a string >>> float_var = 0.0 >>> '%.0f' % float_var if float_var else float_var 0.0 # This is a float
which I am almost certain you did not want.
I guess you are wanting to check for
None
with "if float_var
"? If so, you always spell it "if foo is not None
", not "if foo
", the former being clearer and less bug-prone.If that is what you intended, I suggest you revise your model. Propagating errors by repeatedly returning
None
is a bad thing: it is ugly and bug-prone and non-idiomatic. Use exceptions instead.
Shorter is not always better. Your snippets are not painfully long or unwieldly. In fact, you will want to make them a little longer if you use them to avoid a potential bug.
- Someone may suggest abusing the short-circuiting behavior of
or
for this. However, this makes code harder to read and doesn't allow you to specify betweenNone
and other false values, which often leads to bugs.
- Someone may suggest abusing the short-circuiting behavior of
If you are using are already using v if c else u
you are already using the most readable and efficient ternary operator.
There are other ways but they suffer in readability.
float_var and "%.0f" % float_vav
Isn't it awesome?
精彩评论