为什么这会评估为类型字符串:
>>> strng = 'spam'
>>> type(strng.replace('a', 'A'))
>>> <type 'str'>
但这计算出类型 NoneType?
>>> list_a = ['spam']
>>> type(list_a.append('eggs'))
>>> <type 'NoneType'>
请您参考如下方法:
因为列表上的 append 方法返回 None(或者,换句话说......不返回任何东西),因为它改变了列表。
大多数(如果不是全部的话)python 标准库中的变异方法都是这样做的。
replace 在字符串上,不会发生变化,因为 python 字符串是不可变的。返回一个新的字符串并进行替换。
