Open In App

How to Get a List of Class Attributes in Python?

Last Updated : 13 Jun, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

Getting a list of class attributes in Python means identifying all variables defined at the class level, excluding instance attributes and methods.

For example, a class might have attributes like name, age and location. The output will be a list or dictionary showing these attribute names and their values. Let’s explore some common ways to retrieve these class attributes efficiently.

1. Using vars() function

To find attributes we can also use vars() function. This method returns the dictionary of instance attributes of the given object.

Python
import inspect
class Number :
    one = 'first'
    two = 'second'
    three = 'third'
    
    def __init__(self, attr):
        self.attr = attr
        
    def show(self): 
        print(self.one, self.two, 
              self.three, self.attr)
        
n = Number(2)
n.show()
print(vars(n))

Output
first second third 2
{'attr': 2}

Number has three class attributes and one instance attribute set in __init__. The show() method prints all attributes. Creating n with attr=2 and calling n.show() displays them while vars(n) shows only the instance attributes.

2. Using __dict__ magic method

Every Python object’s __dict__ stores its instance attributes. Like vars() it lets you access an object’s data and its attribute names or values, excluding class attributes and methods.

Python
class Number :
    one = 'first'
    two = 'second'
    three = 'third'
    
    def __init__(self, attr):
        self.attr = attr
        
    def show(self): 
        print(self.one, self.two, 
              self.three, self.attr)
        
n = Number(2)
n.show()

print(n.__dict__)
print(n.__dict__.keys())    # Only attribute names
print(n.__dict__.values())  # Only attribute values

Output
first second third 2
{'attr': 2}
dict_keys(['attr'])
dict_values([2])

show() method prints class and instance attributes. Creating n with attr=2 and calling n.show() displays them. n.__dict__ holds only instance attributes, with .keys() and .values() showing their names and values.

3. Using __slots__

Some classes use __slots__ to limit attributes, saving memory and preventing extra ones. With __slots__, the class has no __dict__ but you can see allowed attributes by checking __slots__

Python
class MyClass:
    __slots__ = ['x', 'y']
    
    def __init__(self, x, y):
        self.x = x
        self.y = y
m = MyClass(10, 20)
print(MyClass.__slots__)
print(f"x = {m.x}, y = {m.y}")

Output
['x', 'y']
x = 10, y = 20

MyClass uses __slots__ to limit attributes to x and y, saving memory by removing __dict__. Creating m with values 10 and 20 lets you access these attributes and MyClass.__slots__ lists them.

4. Using getmembers() Method

Another way of finding a list of attributes is by using the module inspect. This module provides a method called getmembers() that returns a list of class attributes and methods.

Python
import inspect
class Number :
    one = 'first'
    two = 'second'
    three = 'third'
    
    def __init__(self, attr):
        self.attr = attr
        
    def show(self): 
        print(self.one, self.two, 
              self.three, self.attr)
n = Number(2)
n.show()

for i in inspect.getmembers(n):
    if not i[0].startswith('_'):
        if not inspect.ismethod(i[1]): 
            print(i)

Output
first second third 2
('attr', 2)
('one', 'first')
('three', 'third')
('two', 'second')

show() method prints all attributes. After creating n with attr=2 and calling n.show(), inspect.getmembers() retrieves all members, then filters and prints only public, non-method attributes.


Next Article

Similar Reads