base = 1024 if (gnu or binary) else 1000
Rhodri James
rhodri at kynesim.co.uk
Mon Aug 5 11:41:35 EDT 2019
On 05/08/2019 16:01, Hongyi Zhao wrote:
> Hi,
>
> I read the source code of of `humanize/filesize.py' and find the 24 line
> writing as follows:
>
> base = 1024 if (gnu or binary) else 1000
>
> It seems this is not the standard usage of if ... else ....
It's a perfectly standard conditional *expression* (see
https://docs.python.org/3/reference/expressions.html#conditional-expressions),
not an if *statement* at all. It's the equivalent of "?:" in C and
C-like languages.
In general conditional expressions look like:
<true-value> if <condition> else <false-value>
Python evaluates the <condition>. If it is true, the expression
evaluates to <true-value>. If it is false, it evaluates to
<false-value>. In this specific case, "base" is assigned 1024 if "gnu"
or "binary" are true, else it is assigned 1000.
--
Rhodri James *-* Kynesim Ltd
More information about the Python-list
mailing list