为什么在布尔上下文中list.append的计算结果为false? [英] Why does list.append evaluate to false in a boolean context?

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

问题描述

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

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

推荐答案

大多数使容器就地变异的Python方法都返回None -是

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天全站免登陆