-1

here is my example

class MyClass(Enum):
    x=[1,3,5]
    y=[2,5,7]
    w=[33,49]

I would like to write method that will give me a list of ALL lists from enum. For that example it should return

[[1,3,5], [2,5,7], [33,49]]

I tried something like this:

listWithValues= [ z.value for z in MyClass]

But as you can guess it didnt work. Thanks for any helpful advices.

Mateusz
  • 1,163
  • 2
  • 13
  • 25

2 Answers2

1

Here is a full example of what you want. This method will always return every list in the enumeration and ignore every other variable.

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())
Nether
  • 1,081
  • 10
  • 14
0

From the comments it sounds like you want a method on the class that will return a list of all the values. Try this:

    @classmethod
    def all_values(cls):
        return [m.value for m in cls]

and in use:

>>> MyClass.all_values()
[[1, 3, 5], [2, 5, 7], [33, 49]]
Ethan Furman
  • 63,992
  • 20
  • 159
  • 237