How to print all the current properties and values of an object in Python. There are several built-in Python functions and modules that allow you to do this. In this article, we will explore different methods for printing all the current properties of an object with examples.
1. Quick Examples of Printing Properties and Values of Object
These examples will give you high-level idea of how to print all the current properties and values of an object? We will discuss each method in more detail later on.
# Quick Examples of Printing Properties and Values of Object
# Method 1: Using vars() mixed with pprint()
obj_vars = vars(obj)
pprint(obj_vars)
# Method 2: Using dir()
for attr in dir(obj):
print(f"{attr}: {getattr(obj, attr)}")
# Method 3: Using inspect.getmembers()
props = inspect.getmembers(obj, lambda a: not(inspect.isroutine(a)))
for prop in props:
print(prop)
2. Printing Object Properties with vars() and pprint()
You can use the python function vars()
and pprint()
to print current object properties and values in Python. The vars()
returns a dictionary containing the object’s attributes and their current values.
We can then use the pprint()
function to print the dictionary in a human-readable format. The vars()
function only works on objects that have a __dict__
attribute, which includes most user-defined objects but not built-in types like strings or integers.
# Using vars() and pprint() functions
from pprint import pprint
# Class for demonstration
class MyClass:
def __init__(self):
self.prop1 = "value1"
self.prop2 = "value2"
# Create object
obj = MyClass()
# Call the vars() on Object
obj_vars = vars(obj)
# Print the properties
pprint(obj_vars)
This example yields the below output.
# Output:
{'prop1': 'value1', 'prop2': 'value2'}
We will use the above generic class (MyClass
) for all our examples.
3. Printing Object Properties and Values with dir()
Alternatively, you can also use the dir()
method, The dir()
method in Python returns a list of the object’s attributes, including methods and properties. We can then loop through the list and print each attribute and its value.
# Using the dir() function to
# Get Current properties and values
obj = MyClass()
for attr in dir(obj):
# Getting rid of dunder methods
if not attr.startswith("__"):
print(attr, getattr(obj, attr))
Yields the below output:
# Output:
method1 <bound method MyClass.method1 of >
prop1 value1
prop2 value2
The dir()
function returns a list of strings, so it may be necessary to use getattr()
or another method to get the actual value of each attribute.
4. Using inspect.getmembers()
The inspect
module in Python provides several functions for intercepting objects, including getmembers()
. This function returns a list of all the object’s attributes and their current values, wrapped in (name, value)
tuples. We can then loop through the list and print each tuple.
See the below example:
# Using inpect module to get current prop
import inspect
obj = MyClass()
# Using for loop
for attr in inspect.getmembers(obj):
# Avoiding dunder methods
if not attr[0].startswith("__"):
print(attr)
Yields the below output:
# Output:
('method1', <bound method MyClass.method1 of >)
('prop1', 'value1')
('prop2', 'value2')
('prop3', 'value3')
5. Summary and Conclusion
In this article, we have learned several ways to print all the current properties and values of an object in Python. These include using vars()
mixed with pprint()
, dir()
, vars()
, and inspect.getmembers()
. I hope this article was helpful. If you have any questions, please leave them in the comment section.
Happy Coding!
Related Articles
- Python Class attribute Properties
- Python Objects Explained
- Null Object in Python
- Convert JSON Object to String in Python
- Explain Classes & Objects in Python
- Check object has an attribute in Python
- Check Given Object is a List or not in Python
- Different ways to Write Line to File in Python
- What is the use of “assert” in Python?
- Static or Class Variables in Python?
- Python Remove a Trailing New Line
- Python Print without a Newline
- Python print() Function With Examples