1. Introduction
In Python, str and repr are two methods that are used to represent objects as strings. The str function is meant to return representations of objects that are readable and appealing to human eyes. In contrast, repr is used to produce a string representation of an object that is more technical and is often valid Python code that can be used to recreate the object.
2. Key Points
1. Audience: str is for a human-readable representation, repr for a developer-centric and detailed representation.
2. Purpose: str emphasizes readability, repr emphasizes accuracy, and sometimes includes more detail.
3. Return Value: str might be less detailed, repr aims to return a string that, when passed to eval(), could recreate the object.
4. Default Implementation: If not defined, str uses repr, while repr needs explicit implementation.
3. Differences
Aspect | str | repr |
---|---|---|
Audience | Human-readable | Developer-centric |
Purpose | Emphasizes readability | Emphasizes accuracy |
Return Value | Maybe less detailed | Can recreate the object |
Default Implementation | Uses repr if not defined | Needs explicit implementation |
4. Example
class MyObject:
def __init__(self, name):
self.name = name
def __str__(self):
return f'MyObject named {self.name}'
def __repr__(self):
return f'MyObject({self.name!r})'
obj = MyObject('Python')
# Using str
str_output = str(obj)
# Using repr
repr_output = repr(obj)
Output:
str Output: MyObject named Python repr Output: MyObject('Python')
Explanation:
1. The str output provides a readable description, showing just the name of the object.
2. The repr output looks like the Python code to recreate the object, showing the class name and the name in a format that could be used to reconstruct the object.
5. When to use?
- Use str when you need a readable representation of an object, like in print statements or in user interfaces.
- Use repr when you need an unambiguous representation of an object that is more for debugging and development, potentially including more detailed information about the object.
Related Python Posts:
Difference Between Local and Global Variables in Python
Difference Between List and Tuple in Python
Difference Between Array and List in Python
Difference Between List and Dictionary in Python
Difference Between List, Tuple, Set and Dictionary in Python
Difference Between a Set and Dictionary in Python
Difference between for loop and while loop in Python
Difference Between pass and continue in Python
Difference Between List append and extend in Python
Difference Between == and is operator in Python
Difference Between Deep and Shallow Copy in Python
Class Method vs Static Method in Python
Class Method vs Instance Method in Python
Difference Between List and Set in Python
Difference Between Generator and Iterator in Python
Difference Between str and repr in Python
Method Overloading vs Overriding in Python
Difference Between Dictionary and Tuple in Python
Difference Between Dictionary and Object in Python
Difference Between Mutable and Immutable in Python
Difference Between Interface and Abstract Class in Python
Comments
Post a Comment
Leave Comment