在python或coffeescript中,为什么list.append不返回列表本身?如果是这样,递归可以简单得多

时间:2021-08-04 12:19:52

consider the following code if insert() return the list itself.

如果insert()返回列表本身,请考虑以下代码。

def sieve(l):
  if not len(l):
    return []
  return sieve(filter(lambda x: x%l[0] != 0, l)).insert(0, l[0])

For now, we have to rely on a helper function to return the list after insertion.

目前,我们必须依靠辅助函数在插入后返回列表。

def cons(a, l):
  l.insert(0, a)
  return l

def sieve(l):
  if not len(l):
    return []
  return cons(l[0], sieve(filter(lambda x:x%l[0] != 0, l)))

The point of mutable/immutable object is totally valid.

可变/不可变对象的要点是完全有效的。

However, for lists that are mutable, IMHO, append() API could take one more step to return the list itself, rather than not returning anything. Java StringBuilder is a good example. I can do chained append on stringbuilder object recursively....Just wish we could have this here also.

但是,对于可变的列表,IMHO,append()API可能需要再多一步来返回列表本身,而不是返回任何内容。 Java StringBuilder就是一个很好的例子。我可以递归地对stringbuilder对象进行链接附加....希望我们也可以在这里使用它。

3 个解决方案

#1


14  

In Python, it is a big deal to get beginners to learn which objects are “immutable” and cannot be changed, and which are “mutable” and can be altered — in the latter case, every reference to the object sees the same change. This really seems to confuse newcomers. “I innocently called this function I wrote, and suddenly my copy of the list is changed too!”

在Python中,让初学者了解哪些对象是“不可变的”并且无法更改,哪些是“可变的”并且可以被更改是一件大事 - 在后一种情况下,对对象的每次引用都会看到相同的变化。这似乎让新人感到困惑。 “我无辜地称这个函数是我写的,突然我的列表副本也改变了!”

So Python has a convention: immutable objects, when you ask them to make an adjustment, return the newly-created object that is the answer — so:

所以Python有一个约定:不可变对象,当你要求它们进行调整时,返回新创建的对象,这就是答案 - 所以:

a = 'my string'
b = a.replace('y', 'e')

makes b get an entirely new string while a keeps its original value. Obviously such methods must return a value, since you cannot see the change, ever, by inspecting the original, immutable object itself.

使b得到一个全新的字符串,而a保持其原始值。显然,这样的方法必须返回一个值,因为通过检查原始的不可变对象本身,你无法看到变化。

But when you ask a mutable object to change itself, it does not return itself, because it does not need to — you can see the change just by looking at the original object again! This is a critical semantic signal in Python: if a method like append() does not return a new object, then you can see the change just by looking at the old object, and so can everyone else with a reference to the old object.

但是当你要求一个可变对象改变它自己时,它不会返回它自己,因为它不需要 - 你可以通过再次查看原始对象来看到更改!这是Python中的一个关键语义信号:如果像append()这样的方法没有返回一个新对象,那么只需查看旧对象就可以看到更改,其他人也可以通过引用旧对象来查看。

#2


4  

Since you asked about CoffeeScript...

既然你问过CoffeeScript ......

The push method modifies the array in-place. Rather than returning the newly modified array, it returns the value you just added. That's unintuitive, granted, but it can be useful. For instance, you can write things like

push方法就地修改数组。它不是返回新修改的数组,而是返回刚刚添加的值。这是不直观的,被授予,但它可能是有用的。例如,你可以写像

getNewValue -> cache.push fetchValue()

which, in one line, fetches a value, adds it to the cache, and returns it from getNewValue.

在一行中,获取一个值,将其添加到缓存中,并从getNewValue返回它。

The concat method, on the other hand, doesn't modify the original array, instead returning the modified copy. It's supposed to be used for concatenating two arrays, but non-array values will be coerced, so you can use it as a push substitute if you want:

另一方面,concat方法不会修改原始数组,而是返回修改后的副本。它应该用于连接两个数组,但非数组值将被强制转换,因此如果您需要,可以将其用作推送替换:

arr = [1, 2, 3]
arr = arr.concat 4
console.log arr  # [1, 2, 3, 4]

Full documentation on JavaScript's array methods is available on MDN.

MDN上提供了有关JavaScript数组方法的完整文档。

#3


0  

Why not just create a reference? I think that will make the code more readable as well.

为什么不创建一个参考?我认为这将使代码更具可读性。

#1


14  

In Python, it is a big deal to get beginners to learn which objects are “immutable” and cannot be changed, and which are “mutable” and can be altered — in the latter case, every reference to the object sees the same change. This really seems to confuse newcomers. “I innocently called this function I wrote, and suddenly my copy of the list is changed too!”

在Python中,让初学者了解哪些对象是“不可变的”并且无法更改,哪些是“可变的”并且可以被更改是一件大事 - 在后一种情况下,对对象的每次引用都会看到相同的变化。这似乎让新人感到困惑。 “我无辜地称这个函数是我写的,突然我的列表副本也改变了!”

So Python has a convention: immutable objects, when you ask them to make an adjustment, return the newly-created object that is the answer — so:

所以Python有一个约定:不可变对象,当你要求它们进行调整时,返回新创建的对象,这就是答案 - 所以:

a = 'my string'
b = a.replace('y', 'e')

makes b get an entirely new string while a keeps its original value. Obviously such methods must return a value, since you cannot see the change, ever, by inspecting the original, immutable object itself.

使b得到一个全新的字符串,而a保持其原始值。显然,这样的方法必须返回一个值,因为通过检查原始的不可变对象本身,你无法看到变化。

But when you ask a mutable object to change itself, it does not return itself, because it does not need to — you can see the change just by looking at the original object again! This is a critical semantic signal in Python: if a method like append() does not return a new object, then you can see the change just by looking at the old object, and so can everyone else with a reference to the old object.

但是当你要求一个可变对象改变它自己时,它不会返回它自己,因为它不需要 - 你可以通过再次查看原始对象来看到更改!这是Python中的一个关键语义信号:如果像append()这样的方法没有返回一个新对象,那么只需查看旧对象就可以看到更改,其他人也可以通过引用旧对象来查看。

#2


4  

Since you asked about CoffeeScript...

既然你问过CoffeeScript ......

The push method modifies the array in-place. Rather than returning the newly modified array, it returns the value you just added. That's unintuitive, granted, but it can be useful. For instance, you can write things like

push方法就地修改数组。它不是返回新修改的数组,而是返回刚刚添加的值。这是不直观的,被授予,但它可能是有用的。例如,你可以写像

getNewValue -> cache.push fetchValue()

which, in one line, fetches a value, adds it to the cache, and returns it from getNewValue.

在一行中,获取一个值,将其添加到缓存中,并从getNewValue返回它。

The concat method, on the other hand, doesn't modify the original array, instead returning the modified copy. It's supposed to be used for concatenating two arrays, but non-array values will be coerced, so you can use it as a push substitute if you want:

另一方面,concat方法不会修改原始数组,而是返回修改后的副本。它应该用于连接两个数组,但非数组值将被强制转换,因此如果您需要,可以将其用作推送替换:

arr = [1, 2, 3]
arr = arr.concat 4
console.log arr  # [1, 2, 3, 4]

Full documentation on JavaScript's array methods is available on MDN.

MDN上提供了有关JavaScript数组方法的完整文档。

#3


0  

Why not just create a reference? I think that will make the code more readable as well.

为什么不创建一个参考?我认为这将使代码更具可读性。