我具有此功能:
def create_blank_curl_params(urls):
curl_params_list = []
for url in urls:
curl_params = CurlParameters
curl_params.url = url
curl_params_list.append(curl_params)
numbers = []
for number in [1, 2, 3, 4, 5]:
new_number = number
numbers.append(new_number)
return curl_params_list
其中CurlParameters只是一个对象。现在我的数字列表(供参考)看起来很棒,显示为[1,2,3,4,5]。但是,我的curl_params_list只是5个重复元素。好像每次附加(curl_params)都会替换所有其他内容,该如何解决呢?
答案 0 :(得分:0)
理想情况下,您将创建一个新对象。如果您只能使用CurlParameters
对象,则需要为其创建一个新副本:
import copy
def create_blank_curl_params(urls):
curl_params_list = []
for url in urls:
curl_params = copy.deepcopy(CurlParameters)
curl_params.url = url
curl_params_list.append(curl_params)
numbers = []
for number in [1, 2, 3, 4, 5]:
new_number = number
numbers.append(new_number)
return curl_params_list
答案 1 :(得分:0)
我找到了解决方案,问题出在线路上
curl_params = CurlParameters
首先,CurlParameters类没有 init 函数 其次,应该是
curl_params = CurlParameters()