为什么list.sort()不返回列表引用而不是None? [英] Why list.sort() don't return the list reference instead of None?

查看:110
本文介绍了为什么list.sort()不返回列表引用而不是None?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

L = [4,3,2,1]

L = L.sort()

L将引用无,为什么L.sort()don不要回L?

我想问为什么Python的设计师会这样做?

解决方案

< blockquote> an****@gmail.com 写道:

L = [4,3, 2,1]
L = L.sort()
L会引用None,为什么L.sort()不会返回L?
我想问为什么设计师Python会这样做吗?



http://www.python.org/doc/faq/genera...he-sorted-list
< br $> b $ b -

Robert Kern


我开始相信整个世界都是一个谜,一个无害的谜团<由于我们疯狂地试图将它解释为一个潜在的真相,因为它是可怕的。

- Umberto Eco


an****@gmail.com 写道:

L = [4,3,2,1]
L = L.sort()
L将引用None,为什么L.sort()不会返回L?
我想问为什么Python的设计者会这样做?




因为这是表示Python方法的约定

改变对象而不是返回一个新对象。


-

Erik Max Francis&& ma*@alcyone.com && http://www.alcyone.com/max/

美国加利福尼亚州圣何塞市&& 37 20 N 121 53 W&& AIM erikmaxfrancis

坐在书房和/看着手机,好像欠它/欠我一样

a favor - Blu Cantrell


"an****@gmail.com" <一个**** @ gmail.com>写道:

我想问为什么Python的设计师会这样做?




我不是Python核心开发人员或设计师,但我一直都知道

sort()是一个就地排序,因为列表是一个可变对象,它会改变

列表发送sort()信息。如果你想找回一个已排序的可迭代的

使用......排序:)


L = [3,1,2]

ls =已排序(L)


现在ls是您的清单,已分类。


-

劳伦斯 - http://www.oluyede.org/blog

没有什么比一个想法更危险

如果它是你唯一拥有的那个 - E. A. Chartier


L = [4,3,2,1]
L=L.sort()
L will refer to None, why L.sort() don''t return the L?
I want to ask why the designer of Python do so?

解决方案

an****@gmail.com wrote:

L = [4,3,2,1]
L=L.sort()
L will refer to None, why L.sort() don''t return the L?
I want to ask why the designer of Python do so?



http://www.python.org/doc/faq/genera...he-sorted-list

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco


an****@gmail.com wrote:

L = [4,3,2,1]
L=L.sort()
L will refer to None, why L.sort() don''t return the L?
I want to ask why the designer of Python do so?



Because that''s the convention that signifies that a Python method
mutates the object rather than returns a new one.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
Sitting in the den and / Looking at the phone as if it owed / Owed me
a favor -- Blu Cantrell


"an****@gmail.com" <an****@gmail.com> writes:

I want to ask why the designer of Python do so?



I''m not a Python core developer nor a designer but I''ve always known that
sort() is a in-place sort and since the list is a mutable object it mutates the
list sending the "sort()" message. If you want to get back a sorted iterable
use... sorted :)

L = [3, 1, 2]
ls = sorted(L)

now ls is your list, sorted.

--
Lawrence - http://www.oluyede.org/blog
"Nothing is more dangerous than an idea
if it''s the only one you have" - E. A. Chartier


这篇关于为什么list.sort()不返回列表引用而不是None?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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