import enum
class MyClass(enum.Enum):
x = [1, 2, 3]
y = [4, 5, 6]
z = "I am not a list"
w = ["But", "I", "Am"]
@classmethod
def get_lists(cls):
""" Returns all the lists in the Enumeration"""
new_list = []
for potential_list in vars(cls).values(): # search for all of MyClass' attributes
if (isinstance(potential_list, cls) # filter out the garbage attributes
and isinstance(potential_list.value, list) # only get the list attributes
and len(potential_list.value) != 0): # only get the non-empty lists
new_list.append(potential_list.value)
return new_list
print(MyClass.get_lists())
从注释来看,您似乎希望类上有一个方法,该方法将返回所有值的列表。试试这个:
使用中:
^{pr2}$这里有一个完整的例子说明你想要什么。此方法将始终返回枚举中的每个列表,并忽略所有其他变量。在
相关问题 更多 >
编程相关推荐