-: Hi guys and welcome back.
In this video we're going to learn about type hinting.
Type hinting is a new feature in Python 3.5.
So you must be using that Python version or later
in order to be able to use this.
Here we've got a function called list average.
And that takes in a sequence
and returns the sum of the sequence divided by the length.
You can call this with pretty much anything you want.
Right, you can do list a-v-g with one, two, three.
Obviously this is not a sequence
and it's certainly not a list.
So maybe if you're calling this
you'll realise that it doesn't make much sense.
But you can call it.
It will give you an error if you do that.
As you can see,
it tells you that you can't do sum of an integer
or len of an integer.
But while you're coding,
there's nothing there to tell you
that you might be making a mistake.
So let's fix that.
You can write inside the here, inside the brackets,
colon list.
And what this tells Python is that
this thing should be a list.
After the brackets you can type
dash greater than, making a small arrow there, float.
And what that tells Python is that
this function is going to return a float.
So this is how you do type hinting in Python.
However let me say that using the list object as a hint here
is usually not recommended.
Usually you'll be doing from typing import list.
And I know we haven't looked at importing
in Python just yet.
We're going to learn about that in the next video actually.
But you'll be using that
and then you'll be putting that there.
That's the most common way of doing it.
We're going to learn about import in the next video.
So this tells you that the sequence is a list of elements.
You don't know what elements.
And it returns a float.
Notice though that VSCode,
which is the editor that I'm using now,
doesn't tell me that I could be making a mistake.
If you're using PyCharm
or if you're running a programme called a linter,
then that programme or PyCharm will tell you,
hey you're passing in a number here,
but the expectation is that you should pass a list.
So that is why we do type hinting.
You can type a hint anywhere.
So for example, you can type hint your class book.
You can say that the name should be a s-t-r for a string
and the page count should be an int.
You don't have to specify a return value
because this function doesn't return anything.
Here we've got our from typing import list again.
You can also import Tuple, Set and there's a bunch more.
And you can use it like that.
If you wanted to define that the books parameter
should be a list of book objects.
Given that the class book exists of course.
So this is how you can do that.
Also notice that I have signalled a string being returned
from the s-t-r function
and again that's that being used there.
So the key benefit of doing this sort of stuff
is that you'll get told if you pass in the wrong thing.
As we learn more about Python,
then we'll be using these type hints more and more.
So you'll see me using them every now and then
in the next few videos.
One more example, that we've already seen,
our book that has the class methods for hardcover
and paperback.
Now we can type hint it.
And make sure that when we are using this code
we get told whether the name, book type and weight
arguments that we're using to create a book
are correct or not.
So here we're saying that the name should be a string.
Name, colon, string.
Book type should be a string.
And the weight should be an integer.
The repr function returns a string.
And the hardcover and paperback methods take in a string
and an integer and return a book object.
This is how you signal that these methods
return an object of the same type that you're currently in.
You can't do this because it will give you an error
because this method here is created
before the class has finished processing.
So this is created while the class is being processed.
So that's why you can't use that there.
So in Python we put them inside quotes
to signal that this method returns a book object
which is the same type that we're currently in.
If this returned a different class,
then you would do that, say if it returned a bookshelf.
Then you can totally do that.
But if it's the same type that you are currently in.
Then you have to signal it like that.
All right, just a few examples of
how to use type hinting in Python.
Again this is a refresher course,
so we're not going into detail of how everything is working.
But we will be using these type hints
over the next few videos
and you'll get more familiar with them.
They're really straight forward.
It's just there to help you as a developer.
They don't stop your code from running.
They don't raise any errors or anything like that.
It's just to help you.
Thanks for joining me in this video.
And I'll see you in the next one.