Detailing Official String Representation With .__repr__()
00:00
Now let’s get into developer-friendly string representations. The __repr__()
, or represent method in Python is used to define the official string representation of an object and show the way the object is constructed in code, mainly for developers and debugging purposes.
00:23
Let’s go back to the Storage
class and implement the __repr__()
method to return a developer-friendly string.
00:33
Since this is the official representation, let’s do return
f"
for format, and you need to include the name of the class, so Storage(
)
, you need the value so in curly brackets, .self.value
, comma, and you also need to include the units and you need to make sure that this will be a string.
01:01
So you can use quotations, curly bracket .self.unit
. Now to access the __repr__
string, let’s print(__repr__(
storage_size))
, and then let’s run the code.
Become a Member to join the conversation.