我按照以下方式宣布了一个类
class A:
def __init__(self, list_1, list_2):
self.list1 = list_1
self.list2 = list_2
def __getattr__(self, item):
if item in self.list1: return "It is in list 1"
elif item in self.list2: return "It is in list 2"
else: return "It is in neither list 1 nor list 2"
Run Code Online (Sandbox Code Playgroud)
这里当我添加__setattr__ self.list1是递归的,因为__getattr__在每次之后调用self.list1并且这种递归是不可阻挡的.你能帮我解决一下吗?我需要这样实现.
谢谢
def get_quantities(table_to_foods):
""" (dict of {str: list of str}) -> dict of {str: int}
The table_to_foods dict has table names as keys (e.g., 't1', 't2', and so on) and each value
is a list of foods ordered for that table.
Return a dictionary where each key is a food from table_to_foods and each
value is the quantity of that food that was ordered.
>>> get_quantities({'t1': ['Vegetarian stew', 'Poutine', 'Vegetarian stew'], 't3': ['Steak pie', 'Poutine', 'Vegetarian stew'], 't4': ['Steak pie', 'Steak …Run Code Online (Sandbox Code Playgroud)