这段代码是测试copy和deepcopy的,但问题不在这里,看注释
import math
import copy
class Delicacy:
def __init__(self,name,price,weight):
self.name=name
self.price=price
self.weight=weight
def __str__(self):
return 'item:{}, price:{}, total weight: {}'.format(self.name,self.price,self.weight)
warehouse = list()
warehouse.append(Delicacy('Lolly Pop', 0.4, 133))
warehouse.append(Delicacy('Licorice', 0.1, 251))
warehouse.append(Delicacy('Chocolate', 1, 601))
warehouse.append(Delicacy('Sours', 0.01, 513))
warehouse.append(Delicacy('Hard candies', 0.3, 433))
new_warehouse = copy.copy(warehouse)
print(warehouse)#打印出来都是<__main__.Delicacy object at 0x000001F0A6E6E6A0>这类具体位置信息,不应该是一个都是string的列表吗
for i in new_warehouse:
if i.weight>300:
i.price=float(round(i.price-i.price*0.2,3))
for item in warehouse:
print(item)
for item in new_warehouse:
print(item)