Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Wait, types in Python! What garbage is this?! Have no fear, though. We can now hint at what types we should be using but they're not required and they're not binding. Let's talk a bit more about where these things came from and what they're for.
[MUSIC]
0:00
Hey y'all, I'm Kenneth,
your Treehouse Python teacher.
0:04
Today I want to talk about an interesting
newer feature in Python that has caused
0:08
a bit of a ruckus.
0:11
Python, like JavaScript and Ruby, and many
other languages, is dynamically typed.
0:12
This means that a variable like name could
change from an int to a string to a list,
0:16
to whatever else it needs to be.
0:20
This is different from languages like
Java and C#, where a variable has a type
0:22
declared when it's created and it will
always hold some data that fits that type.
0:26
But you probably already know this,
so why am I telling you?
0:30
Well, with the acceptance
of some recent PEPs,
0:32
Python has an officially supported
way of doing type hinting.
0:34
It started ten years ago when
PEP 3107 landed in Python 3.0.
0:38
This PEP allowed for function annotations.
0:43
You could provide information about
the parameters that a function took in
0:45
its return value, and
0:49
these would show up in the __annotations__
attribute of the function.
0:50
Then came Mypy,
a static type checker for Python.
0:54
This tool would look for special
formatting in your Python code where you'd
0:57
declare what type a parameter should be,
and
1:00
then it would tell you if you
ever gave it something else.
1:02
This checker and
many others like it didn't and
1:05
won't make Python statically typed.
1:07
They didn't and
don't enforce anything at runtime.
1:09
Instead they're used
during code reviews and
1:12
in build processes to make
sure code fits a standard.
1:14
In Python 3.5, Guido introduced PEP 484,
which officially laid out
1:17
a specification for how these type
annotations should look and behave.
1:21
This is also when the typing
module came into Python,
1:26
which we'll look at more later.
1:29
And for Python 3.6,
PEP 526 gave us variable annotations.
1:30
So now you can annotate parameters
to functions and methods, and
1:35
state what type of data you'd like
a particular variable to hold.
1:39
These PEPs caused quite a stir.
1:42
People jumped to conclusions and declared
that Python was abandoning its dynamic and
1:44
duck type roots.
1:48
Of course, that hasn't and won't happen.
1:49
All of this type hinting is just another
tool to use when you're developing.
1:51
It's especially useful for
larger code bases or
1:54
code where you can't look through
all of the source easily.
1:57
PyCharm supports these
annotations natively and
1:59
will show you warnings
if you violate them.
2:01
If you don't use PyCharm,
the Mypy tool will do the same.
2:03
Like I said,
type hinting isn't going to change Python.
2:07
It's 100% opt in per file, and
even per function or per variable.
2:09
In fact even with the type hint and
a static checker,
2:14
you can send any data you want to
any function or variable you want.
2:16
Well, assuming it won't
cause an exception.
2:19
Like doc strings,
2:21
testing, and naming conventions,
it's a tool to make your work easier.
2:22
It's something to discuss and
decide on with your team.
2:26
If you decide to use it though, I'm sure
will save you more than one headache.
2:28
So let me show you about type hinting.
2:32
We'll start with a really straightforward
example, then talk about generics and
2:33
optionals, and how to use stub files for
2:36
compatibility with older
versions of Python.
2:38
You need to sign up for Treehouse in order to download course files.
Sign up