How to Print Object Attributes in Python
Last Updated :
04 Jun, 2024
In Python, objects are the cornerstone of its object-oriented programming paradigm. An object is an instance of a class, and it encapsulates both data (attributes) and behaviors (methods). Understanding how to access and print the attributes of an object is fundamental for debugging, inspecting, and utilizing these objects effectively. This article will guide you through various methods to print object attributes in Python.
Understanding Object Attributes
Attributes are variables that belong to an object or class. There are two main types of attributes:
- Instance Attributes: Defined in the
__init__
method and are unique to each instance. - Class Attributes: Defined within the class construction and are shared across all instances of the class.
Here’s a quick example:
Python
class Car:
wheels = 4 # Class attribute
def __init__(self, color, model):
self.color = color # Instance attribute
self.model = model # Instance attribute
my_car = Car("red", "Tesla Model S")
In this example, wheels
is a class attribute, while color
and model
are instance attributes.
Printing Object Attributes
There are several ways to print the attributes of an object. Let's explore them:
Using the vars()
Function
The vars()
function returns the __dict__
attribute of an object, which is a dictionary containing the object’s writable attributes.
Python
Output:
{'color': 'red', 'model': 'Tesla Model S'}
Using the __dict__
Attribute
The __dict__
attribute is a built-in attribute that stores the object's attributes in a dictionary format.
Python
Output:
{'color': 'red', 'model': 'Tesla Model S'}
Using the dir()
Function
The dir()
function returns a list of the attributes and methods of an object. This includes built-in attributes and methods, which you might need to filter out.
Python
Output (truncated for brevity):
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', ..., 'color', 'model']
Similar Reads
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 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 Get a List of Class Attributes in Python? 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
3 min read
Print Objects of a Class in Python In object-oriented programming (OOP), an object is an instance of a class. A class serves as a blueprint, defining the structure and behavior of objects, while an instance is a specific copy of the class with actual values. When an object is created, the class is said to be instantiated. All instanc
4 min read
Dynamic Attributes in Python Dynamic attributes in Python are terminologies for attributes that are defined at runtime, after creating the objects or instances. In Python we call all functions, methods also as an object. So you can define a dynamic instance attribute for nearly anything in Python. Consider the below example for
2 min read