为什么要返回? [英] Why return None?
问题描述
对象修改方法似乎是一种相当普遍的模式
返回无 - 但是,这通常非常不方便。
For实例
def f(lst1,lst2):
g((lst1 + lst2).reverse())#不起作用!
你需要说
def f(lst1,lst2):
a = lst1 + lst2
a.reverse()
g(a)
这实际上是在编写Blender脚本时让我的方式很多 - 对于
实例,我不能说移动(Vector([a,b,c])。normalize()),我必须做
a = Vector([a,b,c ])
a.normalize()
移动(a)
但似乎建议练习而不是错误在
Blender API中,因为标准列表就是这样做的。返回自我而不是没有任何缺点吗?
马丁
c
2004年8月25日星期三08:26:26 GMT,Martin DeMello
< ma * **********@yahoo.com>写道:对象修改方法似乎是一种相当常见的模式,以便返回无 - 但是,这通常非常不方便。
list.reverse()修改列表。 python成语是
这些不会返回对修改后的列表的引用。虽然在2.4中注意了
新的list.sorted()方法...
Anthony
Martin DeMello写道:
对于返回None的对象修改方法来说,似乎是一种相当常见的模式 - 但是,这通常非常不方便。
使用newstyle classe你可以继承list类并使一些
函数返回self。
class List(list) :
def sort(self):
list.sort(self)
返回自我
l = List([1,2,4,2,5,2,6])
print l.sort()
[1,2,2,2,4,5,6]
好吧。排序是一个不好的例子,因为你可以使用sorted()insted。
关于Max M
Anthony Baxter< an *** ********@gmail.com>写道:2004年8月25日星期三08:26:26 GMT,Martin DeMello
< ma *********** @ yahoo.com>写道:对象修改方法似乎是一种相当常见的模式,以便返回无 - 但是,这通常非常不方便。
list.reverse()修改列表。 python成语是
这些不会返回对修改后的列表的引用。虽然注意
是的,但为什么呢?我的意思是,返回None还是有优势吗?
返回自我时会有一些固有的危险吗?
martin
It seems to be a fairly common pattern for an object-modifying method to
return None - however, this is often quite inconvenient.
For instance
def f(lst1, lst2):
g((lst1 + lst2).reverse()) # doesn''t work!
you need to say
def f(lst1, lst2):
a = lst1 + lst2
a.reverse()
g(a)
this is actually getting in my way a lot when scripting Blender - for
instance, I can''t say move(Vector([a,b,c]).normalize()), I have to do
a = Vector([a,b,c])
a.normalize()
move(a)
but it seems to be recommended practice rather than a fault in the
Blender API, since the standard list does it. Is there any drawback to
returning self rather than None?
martin
c
On Wed, 25 Aug 2004 08:26:26 GMT, Martin DeMello
<ma***********@yahoo.com> wrote:It seems to be a fairly common pattern for an object-modifying method to
return None - however, this is often quite inconvenient.
list.reverse() modifies the list in place. The python idiom is that
these don''t return a reference to the modified list. Although note the
new list.sorted() method in 2.4...
Anthony
Martin DeMello wrote:
It seems to be a fairly common pattern for an object-modifying method to
return None - however, this is often quite inconvenient.
With newstyle classe you can subclass the list class and make some
functions return self.
class List(list):
def sort(self):
list.sort(self)
return self
l = List([1,2,4,2,5,2,6])
print l.sort()
[1, 2, 2, 2, 4, 5, 6]
Well ok. Sort is a bad example, as you could just use sorted() insted.
regards Max M
Anthony Baxter <an***********@gmail.com> wrote:On Wed, 25 Aug 2004 08:26:26 GMT, Martin DeMello
<ma***********@yahoo.com> wrote:It seems to be a fairly common pattern for an object-modifying method to
return None - however, this is often quite inconvenient.
list.reverse() modifies the list in place. The python idiom is that
these don''t return a reference to the modified list. Although note the
Yes, but why? I mean, is there either an advantage to returning None or
some inherent danger in returning self?
martin
这篇关于为什么要返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!