gpt4 book ai didi

python - 替代append()

转载 作者:行者123 更新时间:2023-11-30 22:38:18 25 4
gpt4 key购买 nike

对于以下添加方法:

def __add__(self, other):
self.str_list.append(other)
self.count += 1
return self.str_list

我如何重写这个而不附加?

附注:

1) str_list是init中声明的list类型的实例变量

2) 没有使用内置函数。

最佳答案

这是一个有趣的脑筋急转弯,挑战是在不使用内置函数的情况下添加到列表中。我只能想到一种方法:

self.str_list = [*self.str_list, other]       

其他方法(例如 list += [other])在技术上使用内置方法 __add__,因此请注意您的目的,无论它是什么。

关于python - 替代append(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43624549/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com