python 列表的问题

这段代码是测试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)


print list实例调用的list的str,list内置的str应该没调用对象的str方法,写个子类继承list,然后这个子类实现str方法可以实现打印


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)

class MyList(list):
    def __str__(self):
        lst=[]
        for i in self:
            lst.append(str(i))
        return str(lst)

warehouse =MyList()# 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)



因为warehouse里面的元素都是对象啊

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632