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