简体   繁体   中英

Is there a shorter way of writing 'if a > b: a = b' in python?

Can you shorten the following example?

if file_size_download > file_size: file_size_download = file_size

It looks quite clumsy to me.

a = min(a, b)

这简洁明了。

As a more generic answer:

a = b if a < b else a

The previous answer works fine in this particular case, however, this can be of use in cases where it's not just a < comparison.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM