基本语言问题 [英] basic language question

查看:62
本文介绍了基本语言问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一段时间,我被这样的事实所困扰,即变异列表方法如

为''append''或''extend''返回None而不是(mutated)列表本身。

这是否有令人信服的理由?我用Google搜索了一下,但没有找到

关于此的任何内容。

谢谢


Stephan

解决方案
斯蒂芬·迪尔写道:在一段时间后,我得到的事实咬伤,即变异的列表方法
诸如 'append''或''extend''返回None而不是(mutated)
列表本身。这是否有令人信服的理由?我用Google搜索,
但是没有找到任何相关信息。
谢谢

Stephan




原因描述为sort()

< http://www.python.org/doc/faq/genera...ort-return-the

-sorted -list>也适用于追加和延伸。


Daniel


Daniel Dittmar写道:

Stephan Diehl写道:

偶尔,我会被这样的事实所困扰,即变异列表方法
如''append''或''extend''返回None而不是(mutated)
列表本身。这是否有令人信服的理由?我用Google搜索了,
但是没有找到任何相关内容。
谢谢

Stephan
为sort()所描述的原因



< http://www.python.org/doc/faq/genera...ort-return-the -sorted-list>也适用于追加和延伸。


感谢您的链接。我没有发现给定的理由真的引人注目,但是,b $ b虽然。 (但这是我的,不是你的问题)

Daniel




Stephan




" Stephan Diehl" < ST *********** @ gmx.net>在消息中写道

news:bj ************* @ news.t-online.com ...

偶尔我被事实所困扰,变异列表
方法如''append''或''extend''返回None而不是(变异)列表
本身。这是否有令人信服的理由?我google了一下,但是没有
找到任何关于此的信息。




有些人,包括GvR,认为就地变异器不应该返回

将它们与返回新对象的方法区别开来

并保持原始状态不变。我认为基于一些

经验的一个论点是,如果两种类型的方法看起来相同,人们将会忘记差异,从而导致模糊的错误。其他人,有信心记住并且不犯错误的能力,想要变异的对象

返回,所以他们链接方法togethers。虽然Python通常是一个

同意成人语言,但这是Guido选择

所谓的更安全选择的地方。

''返回者''可以包含带有函数的无返回变换器或

返回对象的派生类方法,但我从未见过

任何人发布一个完整的模块,例如,所有列表

mutators。


Terry J. Reedy


Once in a while, I get bitten by the fact, that mutating list methods such
as ''append'' or ''extend'' return None instead of the (mutated) list itself.
Is there a compelling reason for that? I googled around, but didn''t find
anything about this.
Thanks

Stephan

解决方案

Stephan Diehl wrote:

Once in a while, I get bitten by the fact, that mutating list methods
such as ''append'' or ''extend'' return None instead of the (mutated)
list itself. Is there a compelling reason for that? I googled around,
but didn''t find anything about this.
Thanks

Stephan



The reasons described for sort ()
<http://www.python.org/doc/faq/genera...ort-return-the
-sorted-list> also apply to append and extend.

Daniel


Daniel Dittmar wrote:

Stephan Diehl wrote:

Once in a while, I get bitten by the fact, that mutating list methods
such as ''append'' or ''extend'' return None instead of the (mutated)
list itself. Is there a compelling reason for that? I googled around,
but didn''t find anything about this.
Thanks

Stephan
The reasons described for sort ()


<http://www.python.org/doc/faq/genera...ort-return-the -sorted-list> also apply to append and extend.
Thanks for the link. I don''t find the given reason really compelling,
though. (but that''s mine, not your problem)

Daniel



Stephan



"Stephan Diehl" <st***********@gmx.net> wrote in message
news:bj*************@news.t-online.com...

Once in a while, I get bitten by the fact, that mutating list methods such as ''append'' or ''extend'' return None instead of the (mutated) list itself. Is there a compelling reason for that? I googled around, but didn''t find anything about this.



Some people, including GvR, think in-place mutators should not return
anything to distinguish them from methods which return a new object
and leave original untouched. One argument, I presume based on some
experience, is that if both types of method look the same, people will
forget difference, leading to obscure bugs. Others, confident in
their ability to remember and not make mistakes, want mutated object
returned so they chain methods togethers. While Python is generally a
consenting-adults language, this is one place where Guido opted for
the supposedly ''safer'' choice.

''Returners'' could wrap no-return mutators with functions or
derived-class methods that do return the object, but I have never seen
anyone post a complete module that does so for, say, all list
mutators.

Terry J. Reedy


这篇关于基本语言问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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