-: Hi guys and welcome back,
in this video we're going to learn about two methods,
the str and the repr methods.
In Python methods with two underscores on each side
are special methods also called magic methods at times,
because Python will call them for you in some situations.
For example when you do person,
Python calls the init method for you,
even though you didn't say
Python dot underscore, underscore init, you know.
So that is why it is a magic method,
you don't have to call it yourself,
Python can do it for you at times.
So let's create a person that I'm gonna call Bob here
and he's gonna be 35.
Here we're creating a person object by calling the class,
then Python calls the underscore, underscore,
init, underscore, underscore method.
Gives us a new empty object that we're calling self,
and then we're setting the name
and the age properties of that object to be our parameters
that are arguments here,
so Bob and 35 will be stored in Bob's name and age.
Now if we print Bob then you may see something
a little bit weird, which is this.
That's less than, underscore, underscore, main,
underscore, underscore, dot, person,
object, at, a bunch of numbers.
Greater than.
So what does this mean?
What python is doing at this point is
when you've told it to print Bob,
Python is going to print out a string
representing the Bob objects that we've created.
And the reason why Python is printing out
a representation of the object,
is because you can't really print an object,
you have to print a string, some characters
and numbers and so on.
So Python is saying that this string
that it printed out is what it thinks represents our object,
this is a default string, and all objects
and all classes you create will look something like that,
unless you change it yourself.
So the purpose of printing out an object can be to fold.
The first purpose can be to return
a nice, easy to read string so that in your applications
you can print out an object
and your users can make use of that.
For example if they wanted to know about this Bob person
then potentially you could print out Bob
and it could show them some nice information about Bob.
The other reason to print out an object
can be for programmers, you can print out an object
so that you can see the objects information
in case you want to recreate the object.
Let's start off with the first option.
We're going to modify Bob so that when we print him out
we'll get some nice information
that our users can make use of.
The way we do that is we do def, underscore, underscore,
str, underscore, underscore.
Then of course the self parameter,
and in we go to the Bobby.
The underscore, underscore, str, underscore, underscore
is a method, that is a magic method
because it has the two underscores on each side,
and this method gets called for you,
when you want to turn your object into a string.
Let me just return hello from here.
Any now we're going to print Bob.
Notice that we get hello out.
Because what Python has done,
is when you ask it to print an object,
Python realises that really what you want
is a string representation of that object since
that's the only thing you can actually
print out in a console.
When you ask it to print an object it is going to
turn that object into a string using the str method,
and then it's going to print that.
So instead of returning hello here,
we could return something a bit nicer.
Run about the goal, remember the goal
when you're using the underscore, underscore,
str, underscore, underscore method is to
print out a nice, easy to read string for users.
So we're going to print something like this,
person, self, dot name, self dot age, years old.
Now if we run this code again,
you see that we get person Bob, 35 years old.
You can remove the person if you'd like.
So as well as the str method, you have another one
called the repr method.
The goal of the repr method is to be unambiguous,
and if possible it should return a string
that allows us to recreate the original object very easily.
So I'm going to return person, self dot name, self dot age.
Like that.
Notice that I'm putting less than and greater than symbols
that is just for looks, it is to tell programmers
that read this string here
that we are printing out an object.
Now if we press play, you can see
that the str methods output is still printed out.
That is because by default when you print out an object
Python assumes that you wanna show it to your users.
You don't want to show the output of the repr method,
you wanna show the output of the str method.
The repr method is used in a few other places
such as the Python debugger
that we will learn about later on.
But usually the str method is used
so if you wanna make sure to test out your repr method,
just comment this one out,
put a couple of hash symbols in front of it
and then press play.
And then you see that you get person, Bob, 35.
And if you really wanted to you can put the quotes in there,
so that when you press play you out string back,
that's even better.
So we've learned about two methods,
that you can implement in your classes,
the str and the repr methods.
The str method is called when you wanna
turn your object into a string
such as when printing it out
or even when doing str of Bob
as we have learned previously with integers.
The repr method is used to print out
an unambiguous representation of an abject
so that you can use that to recreate the object for example,
if you were a programmer looking at that output.
Usually you'll want to implement the str method,
but implementing the repr method can also be quite nice.
And remember you don't have to use these two,
they're only used for representing an object
as a string but you don't have to do that,
if you don't want.
That's it for this video,
thank you for joining me and i'll see you--