Table of Contents
ToggleWhile working with objects in Python, we will often need to inspect, list or access attributes dynamically. Whether we are debugging, building frameworks, or designing dynamic systems, understanding how to get attributes of objects in Python is a must have skill.
In this tutorial we will explore multiple ways to get all attributes of object Python, get attributes of objects by name , and list all attributes and methods of a class with examples for beginners and advanced users.
What Are Attributes in Python?
Everything you see in Python is an object, and each object can have attributes. Attributes are piece of data or methods associated with that object
For example
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p = Person("Alice", 25)
name and age are attributes of the Person object.
Codeprint(dir(p))Output
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'age', 'name']
The dir() function lists all attributes which includes special (dunder) methods like __init__ and user defined attributes like name, age in our above example.
If I summarized for “how to get all attributes of an object Python”, dir() would be my answer.Get Only User-Defined Attributes
If we want only the attributes which the user has defined and not the built-in ones then we can use the __dict__ attribute for that.
Code
print(p.__dict__)
Output
{'name': 'Alice', 'age': 25}
Here is a simple rule
- __dict__ → user-defined attributes
- dir() → all attributes (including built-ins)
This approach is perfect if we are trying to get all attributes and values of object Python without the extra noise.
Python Get All Attributes and Values of Object
If we want both attribute names and their respective values then we can iterate through the dictionary returned by __dict__.
For name, value in p.__dict__items(): print(name, “:”, value)Output
Name: Alice Age: 25
The above technique gives us a clean list of attributes and their values.
Python Get Attributes of Object by Name
Code
attr_name = "name" print(getattr(p, attr_name))Output
Output: Alice
The function getattr(object, name, default) returns the value of the attribute with the given name. If by any chance the attribute doesn’t exists you have a option to prove a default value.
Codeprint(getattr(p, "height", "Not Found"))Output
Not Found
This method works perfectly fine when attributes are dynamic for example these attributes come from user input or database queries.
Python Get All Attributes of Class
class MyClass:
x = 10
y = 20
print(MyClass.__dict__)
Output
{'__module__': '__main__', 'x': 10, 'y': 20, '__dict__':
<attribute '__dict__' of 'MyClass' objects>,
'__weakref__': <attribute '__weakref__'
of 'MyClass' objects>, '__doc__': None}
Above code shows class-level attributes like x and y.
We can also use vars() to do this in a cleaner way:
print(vars(MyClass))Above methods gets all attributes of a class without calling any special methods.
Get Attributes of Bytes Object Python
we can also get attributes of bytes object Python using dir().
Codedata = b"hello" print(dir(data))Output (trimmed):
['__add__', '__class__', '__contains__', '__doc__', '__eq__', 'capitalize', 'center', 'count', 'decode', 'endswith', 'find', 'hex', 'index', 'isalnum', 'isalpha', 'isdigit', 'join', 'lower', 'replace', 'split']
Get Attributes of List object Python
my_list = [1, 2, 3] print(dir(my_list))Output
['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getstate__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
Get attributes of bytes object Python
b = b"Python" print(dir(b))Output
['__add__', '__buffer__', '__bytes__', '__class__', '__contains__', '__delattr__','__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__','__getitem__', '__getnewargs__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'center', 'count', 'decode', 'endswith', 'expandtabs', 'find', 'fromhex', 'hex', 'index', 'isalnum', 'isalpha', 'isascii', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join','ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'removeprefix',
Get attributes of array
import array
arr = array.array('i', [1, 2, 3])
print(dir(arr))
Output
['__add__', '__class__', '__class_getitem__', '__contains__',
'__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__',
'__format__', '__ge__','__getattribute__', '__getitem__',
'__getstate__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__',
'__iter__']
Get attributes of Dictionary
my_dict = {'name': 'John', 'age': 30}
print(dir(my_dict))
Output
['__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__ior__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__ror__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
Summary Table
| Task | Function | Example |
| Get all attributes | dir(obj) | dir(p) |
| Get only user-defined | obj.__dict__ | p.__dict__ |
| Get value by name | getattr(obj, name) | getattr(p, ‘name’) |
| Get class attributes | vars(MyClass) | vars(MyClass) |
| Get both names and values | obj.__dict__.items() | loop over items |
| List methods only | inspect.getmembers(obj, inspect.isfunction) | see methods only |
Getting attributes of object Python helps us for debugging and dynamic programming To summaries:
- dir() to get all attributes of object Python
- __dict__ or vars() for custom attributes
- getattr() for Python get attributes of object by name
- inspect for Python list attributes and methods



