为什么 list.append 在布尔上下文中评估为 false? [英] Why does list.append evaluate to false in a boolean context?

查看:26
本文介绍了为什么 list.append 在布尔上下文中评估为 false?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

list.append 评估结果为假是否有原因?还是只是成功时返回 0 的 C 约定才起作用?

<预><代码>>>>你 = []>>>不是 u.append(6)真的

解决方案

大多数对容器进行就地变异的 Python 方法都返回 None -- 命令查询分离.(Python 对事物总是相当务实,所以一些 mutator 在获取它时确实会返回一个可用的值,否则会很昂贵或一团糟——pop 方法是这种实用主义的一个很好的例子——但是那些绝对是例外,而不是规则,并且没有理由让 append 成为例外).

Is there a reason being list.append evaluating to false? Or is it just the C convention of returning 0 when successful that comes into play?

>>> u = []
>>> not u.append(6)
True

解决方案

Most Python methods that mutate a container in-place return None -- an application of the principle of Command-query separation. (Python's always reasonably pragmatic about things, so a few mutators do return a usable value when getting it otherwise would be expensive or a mess -- the pop method is a good example of this pragmatism -- but those are definitely the exception, not the rule, and there's no reason to make append an exception).

这篇关于为什么 list.append 在布尔上下文中评估为 false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆