How to Get a List of Class Attributes in Python?
Last Updated :
13 Jun, 2025
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))
Outputfirst 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
Outputfirst 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)
Outputfirst 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.
Related articles:
Similar Reads
How to Change Class Attributes in Python In Python, editing class attributes involves modifying the characteristics or properties associated with a class. Class attributes are shared by all instances of the class and play an important role in defining the behavior and state of objects within the class. In this article, we will explore diff
3 min read
How to create a list of object in Python class In Python, creating a list of objects involves storing instances of a class in a list, which allows for easy access, modification and iteration. For example, with a Geeks class having attributes name and roll, you can efficiently manage multiple objects in a list. Let's explore different methods to
3 min read
How to Add Attributes in Python Metaclass? This article explains what a metaclass is in the Python programming language and how to add attributes to a Python metaclass. First, let's understand what a metaclass is. This is a reasonably advanced Python topic and the following prerequisites are expected You have a good grasp of Python OOP Conce
4 min read
How to Access dict Attribute in Python In Python, A dictionary is a type of data structure that may be used to hold collections of key-value pairs. A dictionary's keys are connected with specific values, and you can access these values by using the keys. When working with dictionaries, accessing dictionary attributes is a basic function
6 min read
Python Attributes: Class Vs. Instance Explained In Python, attributes are properties associated with objects. They can be variables or methods that are defined within a class or an instance of a class. Understanding the difference between class and instance attributes is fundamental in object-oriented programming. Here's an explanation: Python At
4 min read