|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
>>> list1 = [1,2,3,'e']
>>> list2 = list1.reverse()
>>> list2 == list1
False
>>> type(list2)
<class 'NoneType'>
>>> list1
['e', 3, 2, 1]
list1被翻转过来了,为啥list2没有被赋予翻转的list1列表呢,我不是有list2 = list1.reverse() 这一条语句吗?
python中有的函数直接修改原始对象,不返回新对象,或者说返回None。list.reverse()不返回新对象。
而有的函数返回一个新对象,可以复制给其他变量。sorted()函数返回新对象。
对于不熟悉的函数,可以help。
- >>> help(list.reverse)
- Help on method_descriptor:
- reverse(...)
- L.reverse() -- reverse *IN PLACE* # 没有说return,就是直接操作原对象。
- >>> help(sorted)
- Help on built-in function sorted in module builtins:
- sorted(iterable, /, *, key=None, reverse=False)
- Return a new list containing all items from the iterable in ascending order. # 返回一个新列表
-
- A custom key function can be supplied to customize the sort order, and the
- reverse flag can be set to request the result in descending order.
- >>> ls1 = [1,2,3]
- >>> print(ls1.reverse())
- None
- >>>
复制代码
|
|