This action will force synchronization from pish7/stdpython, which will overwrite any changes that you have made since you forked the repository, and can not be recovered!!!
Synchronous operation will process in the background and will refresh the page when finishing processing. Please be patient.
import copyclass Taco:def __init__(self, toppings):self.ingredients = copy.copy(toppings) # 浅复制def add_sauce(self, sauce):self.ingredients.append(sauce)default_toppings = ["Lettuce", "Tomato", "Beef"]# 由于构造函数中使用浅复制,对象内部的 ingredients 有单独的副本,而且不会指向 default_toppingsmild_taco = Taco(default_toppings)hot_taco = Taco(default_toppings)hot_taco.add_sauce("Salsa")print(mild_taco.ingredients)print(hot_taco.ingredients)default_toppings = ["Lettuce", "Tomato", "Beef"]mild_taco = Taco(default_toppings)# 这里需要使用深复制来复制对象# 否则复制出来的对象的 ingredients 与 mild_taco.ingredients 将会指向同一个数据hot_taco = copy.deepcopy(mild_taco)hot_taco.add_sauce("Salsa")print(mild_taco.ingredients)print(hot_taco.ingredients)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。