Python vars() Function

Returns the attributes of a class as a dictionary. Contrast with the dir() function, which includes class attributes and the attributes of super classes as well.

Syntax

Python
vars(object = None)

Parameters

ParameterDescription
object Optional. The object for which to return the attributes. If no object is provided then the local symbol table will be returned

Example

Python
class SuperClass():
    b = 6

class SomeClass(SuperClass):
    a = 5
    text = 'Hello World'

    def someFunction(self):
        pass

# All attributes in the class SomeClass
print('All attributes of SomeClass:')
print(vars(SomeClass))

# It may be more useful to exclude the built-in names so you get just the attributes of SomeClass:
print('\nAll attributes that are not built-ins:')
variables = list(filter(lambda prop : not(prop.startswith('__')), vars(SomeClass)))
print(variables)

Output

All attributes of SomeClass:
{'__module__': '__main__', 'a': 5, 'text': 'Hello World', 'someFunction': <function SomeClass.someFunction at 0x0000028D59688708>, '__doc__': None}

All attributes that are not built-ins:
['a', 'text', 'someFunction']