Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to convert an object x to an expression string in Python?
In Python, an expression is any statement that evaluates to a value when executed. Converting an object to an expression string means creating a string representation that can be evaluated back to recreate the original object.
What's an Expression?
Python expressions are code snippets that return a value when evaluated ?
print(4 + 6) print(22172 * 27282) print(max(12, 9330))
10 604896504 9330
We can evaluate a valid Python expression using the eval() function to convert strings back to objects.
Converting Objects to Expression Strings
Python provides several methods to convert objects to expression strings ?
Using the
repr()functionUsing the
str()functionCreating custom
__repr__()methods
Using repr() Function
The repr() function returns an unambiguous string representation that ideally can be evaluated with eval() to recreate the object ?
x = [3, 2, 1]
expr_str = repr(x)
print("Expression string:", expr_str)
print("Type:", type(expr_str))
# Recreate object from expression string
x_copy = eval(expr_str)
print("Recreated object:", x_copy)
print("Objects equal:", x == x_copy)
Expression string: [3, 2, 1] Type: <class 'str'> Recreated object: [3, 2, 1] Objects equal: True
With Dictionary Objects
data = {'a': 2, 'b': 4}
expr_str = repr(data)
print("Expression string:", expr_str)
# Recreate from string
data_copy = eval(expr_str)
print("Recreated object:", data_copy)
Expression string: {'a': 2, 'b': 4}
Recreated object: {'a': 2, 'b': 4}
Using str() Function
The str() function returns a human-readable string representation, but may not always be evaluable ?
import datetime
today = datetime.datetime.now()
print("str() output:", str(today))
print("repr() output:", repr(today))
str() output: 2024-01-15 10:30:45.123456 repr() output: datetime.datetime(2024, 1, 15, 10, 30, 45, 123456)
Custom repr() for Objects
Define __repr__() method to control how objects convert to expression strings ?
class Point:
def __init__(self, a, b):
self.a = a
self.b = b
def __repr__(self):
return f"Point({self.a}, {self.b})"
def __str__(self):
return f"({self.a}, {self.b})"
p = Point(4, 5)
print("repr():", repr(p))
print("str():", str(p))
# Can recreate object from repr() output
expr_str = repr(p)
print("Expression string:", expr_str)
repr(): Point(4, 5) str(): (4, 5) Expression string: Point(4, 5)
Comparison
| Function | Purpose | Evaluable | Best For |
|---|---|---|---|
repr() |
Developer representation | Usually Yes | Debugging, logging |
str() |
Human-readable | Sometimes | User display |
Conclusion
Use repr() to create evaluable expression strings for debugging and object recreation. Use str() for human-readable display. Implement custom __repr__() methods for meaningful object representations.
