I noticed that in Python, when we have an object with the __str__
method, it is responsible for returning a string that represents the object - or something like that.
Example:
from uuid import uuid4
uuid4().__str__()
The output is:
36cdc126-9d4d-43f9-9ede-bef8e15b834c
However, the same happens with calling str
passing that same object as a parameter.
str(uuid4())
The output is:
36cdc126-9d4d-43f9-9ede-bef8e15b834c
I have some questions about this:
-
Is there any difference between calling method
__str__
(my_object.__str__()
relative tostr
(str(my_object)
)?) -
The
__str__
method is used by thestr
function? -
Considering the best practices I should apply to the language, which one should I use to return the object as a string?