In this tutorial we will talk about two more magic methods the SDR method and the art EPR method the
SDR method is call using the built in function SD art or when we are using the print function in Python
and this method the SDR method is used to compute the informal string representation of an object coming
to the RTP add method.
This method is called using the built in function IDE E.P. at and it is used to compute the official
string representation of an object the SDR and the odd add method must return a String object.
Let us now see how these two methods work or function to the SDR function.
I'm going to pass an integer execute and this has returned a string object the number one enclosed in
court.
So this is a string.
Now let us pass a string to the R E P R method let's pass the integer 1 and it has returned a String
object as you can see now we will pass a string to the SDR method.
Let's pass the string hello.
And it tested it on a string object let us parse the same string the same string to the odd EPA method
this time the string Halo has been dropped on enclosed in double quotes by the IDP at Metro.
So what is the difference between the output that we see here that is from the SDR method and the output
here from the RTP method as talked earlier.
The SDR method returns an informal representation of a string so the SDR method is used to print a nicely
printable string representation of an object and the SDR method displays the just the content of these
string coming to the ADP add method.
This computes the official string representation of an object or a literal representation of a string
the string that you see in the output can be evaluated by the Python interpreter into a string object.
The SDR method is used for the users so that the output is a human readable string coming to the IDP
out method.
It is supposed to include detailed information about an object content.
So this method is for the developers and it is used for debugging so the representation should be information
rich.
In a previous tutorial we have created a class card and this class defined these structured object object
which is basically a list of items and then we have created two instances of the card.
Class got one and got to let us now display these two cards using the SD card and the IDE EPR method
and see how the added present.
Let us pass the object got one two D string method and this is how it is represented.
This is the string representation of the instance Scott one
what about RTP add method.
How does it represent a good instance.
Got one.
And this is how it is represented using the odd API meter and the output basically says that this instance
got one is an object of the class card and this particular memory address when we display it when we
want to string representation of our word instance of the instance got one.
We actually want to see the list of items in this particular card.
But this is not happening.
That is because we have not defined how to format the card object.
These two methods SD odd and the IDP add methods are not defined in a very user defined class got so
it only has the init method as you can see here so let us now define two methods in this class.
God is d odd and the IDP our method and get a string representation of our God object.
I want to define the SDR method in such a way that whenever I display the instance of the card plus
got one card do using a print function the output should only display the items in the list.
So we will define another function in this class card and this is called underscored underscored underscored
underscored the double underscored SDR method which is the magic method and this should return a String
object in order to format the string.
I'm going to use the default mat method so Indigo curly braces.
We are going to parse the list of items so dart forward might still dart items.
We have defined the SDR method in our word class got in order to have an informal representation of
the card object.
Now we will also define the double underscored at EPA method in our class got no to compute the official
representation or discard object.
So let's see how we can do that.
This is how we have created the instance of the cat object.
We have parsed a list of items to the cut class now using IDP odd method when we display the instances
of the card object.
This is how we want the object to be represented.
So when we display the card object using the odd EPR method this is how we want the object to be represented.
Before defining the IDP method in our class.
This is how the object is represented in the official string representation.
Now we will define the IDP ad meter in our class card.
So this is the class card.
We will be playing the double underscored IDP ad method.
The odd EPR method should return a String object and the output should look something like this.
So using the format method we are going to pass the list of items in the curly braces here.
So let us add these two methods in the class card and then execute.
So we have added these two methods.
The double underscore the SDR and the double underscored RTP and methods.
In the class card and now let us display these two instances let us pass the instance card to one.
Now when you execute the SDR method by passing the instance of the card object.
So now you see
this has done an informal representation of human readable format.
All the card object now letters call the odd EPR method on the card object or the instance card one.
So this time it has done a string that can be directly executed.
And if you observe carefully this object looks exactly like how the instance was created.
And this brings us to the end of the topic SD card and the IDB added methods which are the magic of
the Dunbar methods.