-2

I want to print all class attributes. Not instance attributes. So I want to get ['list1','list2'] output.

class MyClass():
    list1 = [1,2,3]
    list2 = ['a','b','c']
    def __init__(self, size, speed):
        self.size = size  
        self.speed = speed

I want to get ['list1', 'list2']

Nico Albers
  • 1,556
  • 1
  • 15
  • 32

1 Answers1

0
attributes = [ attr for attr in list(vars(MyClass)) if attr[0] is not '_' and
             type(vars(MyClass)[attr]) is not type(lambda :0) ]

This outputs the following:

print(attributes)
>>> ['list1', 'list2']

If you have a methods in your class, "attributes" will not contain your methods' names:

class MyClass():
        list1 = [1,2,3]
        list2 = ['a','b','c']
        def __init__(self, size, speed):
            self.size = size  
            self.speed = speed
        def energy(self, size, speed):
            return 0.5*size*speed**2

attributes = [ attr for attr in list(vars(MyClass)) if attr[0] is not '_' and
                 type(vars(MyClass)[attr]) is not type(lambda :0) ]

print(attributes)
>>>['list1', 'list2']