python - 字符串与列表类型和NoneType

iwa*_*der 1 python

为什么这会评估为字符串类型:

>>> strng = 'spam'
>>> type(strng.replace('a', 'A')) 
>>> <type 'str'>
Run Code Online (Sandbox Code Playgroud)

但是这个评估类型为NoneType?

>>> list_a = ['spam']
>>> type(list_a.append('eggs')) 
>>> <type 'NoneType'>
Run Code Online (Sandbox Code Playgroud)

Ada*_*ner 5

因为append列表上的方法返回None(或者说,另一种方式......不返回任何内容),因为它会改变列表.

大多数(如果不是全部)python标准库中的变异方法就是这样做的.

replace在字符串上,不会变异,因为python字符串是不可变的.返回一个新字符串并进行替换.