Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Python Type Hinting!
You have completed Python Type Hinting!
Preview
Sometimes you can't specify your types just with the built-in types. In that case, you can us `Optional`, `Union`, and `List` from the `typing` module to specify custom type combinations.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Like I said in the introduction
to this workshop,
0:00
Python 3.5 gave us the typing module.
0:02
This module defines a bunch of,
0:05
well, types that we can use when
creating hints for our code.
0:07
Let's look at something
a bit more complex.
0:10
I've been working on some classes
that all relate to recipes.
0:12
There's an ingredient class that's
already typed it just uses strings but
0:15
the ones in this model
are a bit more complicated.
0:19
For instance the recipe ingredient
class can when instantiated receive
0:22
either an ingredient or
a string to turn into an ingredient.
0:26
Maybe not the best design but
that's what I have to work with.
0:31
In the typing module there
are several types defined for
0:34
these more complex situations but
0:37
there are three of them that you'll find
yourself using more often than the others.
0:38
It just so happens that I need to
use all three of them in here.
0:42
So let me go ahead and import these,
0:45
the first one that I want
to talk about is list.
0:48
So I'm gonna say from typing import list.
0:52
The list type lets you say that you want
a list of items of a particular type.
0:58
For example,
I would want a list of strings,
1:02
if I was annotating the str.join method.
1:04
In my recipe class,
1:07
down here, I want to store ingredients
as a list of recipe ingredients.
1:09
I wanted to start out blank too,
so, hey, you get a twofer.
1:14
So ingredients right now is a list and
I'm gonna say this is gonna
1:19
be a list of RecipeIngredients and
I want it to be a blank list.
1:25
So that's what this equals blank is and
1:30
for instance self.title right
here is going to be a string.
1:32
And I also want to have steps
be a list of recipe steps.
1:38
And this is where we're using Python 3.6's
variable type hinting too, by the way,
1:47
this line right here.
1:51
Just like when we hint function
parameters, we use a colon and
1:53
then the type.
1:55
And if we don't want to define
a default value for the variable,
1:57
we can just name it and hint it, right?
2:00
So, if I had say another one, self.order,
2:02
this is just going to be an int,
all right?
2:07
So, that just makes that one an int.
2:12
But if I want to have a default value or
2:14
I want to assign a value,
I can do with an equal sign afterwards.
2:15
And I don't need that order, so
I'm gonna go ahead and take that out.
2:18
The method where we just have the variable
name and the type ends up looking a lot
2:21
like other languages where you have to
declare a variable before you can use it.
2:25
But this is 100% semantic sugar and
2:28
doesn't actually create
the variable in Python.
2:30
Quick note about lists.
2:33
Much like the actual list type
you don't usually want to
2:34
use it when you're declaring types with
parameters in a function or a method.
2:37
You want to check out the mapping sequence
and abstract set types from the typing
2:42
module for those cases there's a link
to these in the teacher's notes.
2:45
I also wanna make this one add a string.
2:50
All right, so what about recipe ingredient
needing either a string or an ingredient.
2:53
Right right up here that was our thing.
2:58
Well this is where
the union type comes in.
3:01
Union lets you specify two or
more valid types.
3:04
So let's bring in union.
3:07
And then here for ingredient I'm gonna say
3:11
this is a union of ingredient or string.
3:16
So now the type checkers know that either
of these values are valid for the method.
3:20
If you do a union with only one
type Python ignores the union and
3:24
just uses the type that you included.
3:27
Python also flattens
unions If you nest them.
3:29
All right on to the third handy dandy type
that I mentioned this one is optional.
3:31
Not that it is optional.
3:37
It's called optional and it specifies that
a type is well exactly that it's optional.
3:38
This means that the value can be one of
the types that are specified or none.
3:46
This is really good for places where
you have arguments that can be left out
3:52
in fact recipe ingredient
needs exactly that.
3:56
Because measurement here can be none.
4:00
So we'll say that
measurement is optional and
4:03
if it's available if it's included
I want it to be a string.
4:06
Otherwise it's none
condition would be the same.
4:10
The measurable value is optional,
defaults to none.
4:14
Now the type checker knows that it's
okay for the argument to be admitted.
4:15
If you look through the downloads that
are associated with this workshop,
4:19
you'll see this file fully
filled out with types.
4:21
Something you might notice is
that it feels a bit redundant.
4:24
You could just specify the types when
you're defining the method parameters and
4:26
be safe enough.
4:29
But I like to specify the types
when I first define a variable too.
4:30
Just make sure that my thinking follows
the same pattern all the way through again
4:34
this is something to
decide with your team.
4:37
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up