【发布时间】:2021-03-07 19:48:08
【问题描述】:
我有以下分配给变量homeTasks的值列表:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
我有一个 for 循环将 Task 类的对象列表分配给变量 allTasks。
# create empty allTasks list
allTasks=[]
# create thisTask for every value of homeTasks and append to allTasks
for y in range(len(homeTasks)):
thisTask = Task.get_task(homeTasks[y])
allTasks.append(thisTask)
print(allTasks[y].taskName)
# print task names from allTasks for testing:
for z in range(len(allTasks)):
print(z)
print(allTasks[z].taskName)
这给了我以下信息:
Do Dishes
Make Bed
Laundry
Study
Laundry
Elliptical
Elliptical
Laundry
Elliptical
Elliptical
Elliptical
test
0
test
1
test
2
test
3
test
4
test
5
test
6
test
7
test
8
test
9
test
10
test
11
test
由于某种原因,分配给 allTasks 的最后一个对象被分配给列表中的每个索引。 thisTask 的所有属性都是如此。
作为参考,这是我的 Task 类和 get_task 类方法的样子:
class Task(object):
def __init__(self, taskID, taskName, points, active, complete, approved, assignedUserID, createdByUserID, dateCreated, dateCompleted, frequency):
self.taskID = taskID
self.taskName = taskName
self.points = points
self.complete = complete
self.approved = approved
self.assignedUserID = assignedUserID
self.createdByUserID = createdByUserID
self.dateCreated = dateCreated
self.dateCompleted = dateCompleted
self.frequency = frequency
self.homeID = homeID
@classmethod
def get_task(self, taskID):
t = (taskID,)
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM tasks WHERE taskID=%s", t)
columns = [col[0] for col in cur.description]
task = [dict(zip(columns, row)) for row in cur.fetchall()]
self.taskID = taskID
self.taskName = task[0]['taskName']
self.points = task[0]['points']
self.aproved = task[0]['approved']
self.assignedUserID = task[0]['assignedUserID']
self.createdByUserID = task[0]['createdByUserID']
self.dateCreated = task[0]['dateCreated']
self.dateCompleted = task[0]['dateCompleted']
self.frequency = task[0]['frequency']
self.dueDate = task[0]['dueDate']
self.homeID = task[0]['homeID']
return self
我知道该方法工作正常,因为 thisTask 每次循环迭代时都有正确的值。 .append 似乎不起作用,因为一旦我退出循环,allTasks 中的值就是错误的。
编辑:根据@barny,我为此创建了一个最小的可重现示例:
import copy
# initialize Task object
class Task(object):
def __init__(self, taskID, taskName, points, active, complete, approved, assignedUserID, createdByUserID, dateCreated, dateCompleted, frequency):
self.taskID = taskID
self.taskName = taskName
# define method to return values
@classmethod
def get_task(self, index):
taskIDList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
taskNames = ['Do Dishes', 'Make Bed', 'Laundry', 'Study', 'Laundry', 'Elliptical', 'Elliptical', 'Laundry', 'Elliptical', 'Elliptical', 'Elliptical', 'test']
self.taskID = taskIDList[index]
self.taskName = taskNames[index]
return self
# create empty allTasks list
allTasks=[]
homeTasks = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
# create thisTask for every value of homeTasks and append to allTasks
for y in range(len(homeTasks)):
thisTask = Task.get_task(y)
allTasks.append(thisTask)
print(allTasks[y].taskName)
# print task names from allTasks for testing:
for z in range(len(allTasks)):
print(z)
print(allTasks[z].taskName)
另外,根据反馈,我现在了解到thisTask 是对对象的引用,而不是get_task 结果的新副本。为此,我尝试在Task.get_task(homeTasks[y]) 和thisTask 上使用copy.copy() 和copy.deepcopy() 均无济于事。似乎连副本都是参考。有没有其他方法可以解决这个问题?
编辑 2:以下是我尝试使用复制和深度复制的方式:
for y in range(len(homeTasks)):
print(y)
thisTask = copy.copy(Task.get_task(y))
allTasks.append(thisTask)
print(allTasks[y].taskName)
for y in range(len(homeTasks)):
print(y)
thisTask = copy.deepcopy(Task.get_task(y))
allTasks.append(thisTask)
print(allTasks[y].taskName)
for y in range(len(homeTasks)):
print(y)
thisTask = Task.get_task(y)
allTasks.append(copy.copy(thisTask))
print(allTasks[y].taskName)
for y in range(len(homeTasks)):
print(y)
thisTask = Task.get_task(y)
allTasks.append(copy.deepcopy(thisTask))
print(allTasks[y].taskName)
在每种情况下都是我的测试:
for z in range(len(allTasks)):
print(z)
print(allTasks[z].taskName)
所有 4 个都产生了完全相同的结果:
0
Do Dishes
1
Make Bed
2
Laundry
3
Study
4
Laundry
5
Elliptical
6
Elliptical
7
Laundry
8
Elliptical
9
Elliptical
10
Elliptical
11
test
0
test
1
test
2
test
3
test
4
test
5
test
6
test
7
test
8
test
9
test
10
test
11
test
【问题讨论】:
-
请将所有相关代码(包括导入和数据)以minimal reproducible example 的形式编辑到您的问题中 - 这样任何人都可以从您的问题中复制一份并粘贴到文件中,并且无需添加任何内容 运行它以查看与您提出的问题相同的结果。
-
好的,现在应该可以重现了。谢谢你告诉我。
-
您如何尝试制作/使用
Task的副本? -
我刚刚编辑了我的主要帖子并粘贴了我尝试过的 4 种方法以及我得到的输出。抱歉,之前应该这样做的。
标签: python list class for-loop object