>>> items = [1, 10, 7, 4, 5, 9] >>> def sum(items): ... head, *tail = items ... return head + sum(tail) if tail else head >>> sum(items) 36 >>>
谁能帮忙解释下为什么第四行的时候要用:
if tail else head
这是什么意思呢?详细讲述下过程,多谢。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
http://wangye.org/blog/archives/690/