[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

base = 1024 if (gnu or binary) else 1000

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 
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