【发布时间】:2022-01-23 16:02:57
【问题描述】:
我是编程和 Stackoverflow 的新手。我目前正在研究文件处理和类。该程序正在运行,但我的问题是这部分代码是否可以接受? list_of_objects 是一个带有“[]”的列表,其中包含类对象。
def menu_choice_function(list_of_objects):
while True:
menu()
choice = input("Choice: ")
if choice == "1":
print("Create a packing list")
list_of_objects = create_packing_list(list_of_objects)
elif choice == "2":
print("Show the packing list content")
show_packing_list_content(list_of_objects)
elif choice == "3":
print("Append or delete items in the packing list")
list_of_objects = app_del_items(list_of_objects)
elif choice == "4":
print("Show all packing lists")
show_all_packing_list(list_of_objects)
elif choice == "5":
print("Edit the packing list name or date")
list_of_objects = edit_name_date(list_of_objects)
elif choice == "6":
print("Remove a packing list")
list_of_objects = remove_packing_list(list_of_objects)
elif choice == "7":
print("Add reminder or remove reminder for the packing list")
list_of_objects = add_remove_reminder(list_of_objects)
elif choice == "8":
return list_of_objects
else:
print("Please enter a choice.")
如果可以接受,我的意思是您可以看到一些函数返回一个修改后的列表,但它们都与参数变量具有相同的名称。我这样做是因为我认为您可以修改列表变量。但我想知道这种方法是否会带来一些后果。到目前为止它正在工作,因为到目前为止程序中没有错误或奇怪的行为。对不起,我的英语不好。我希望你明白我想问什么。
【问题讨论】:
-
如果 list_of_objects 是 Python 列表,则无需返回对它的引用除非您正在函数中创建一个新列表作为原始列表的替换。你的函数 menu() 做什么?