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
    
      
  The simplest type hinting is by using the built-in types. Let's see how to hint functions that are only concerned with the built-in types.
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
                      I'm gonna to be using Python 3.6 and
PyCharm for all of these examples.
                      0:00
                    
                    
                      If you're still on Python 3.5, you'll
be able to follow along with everything
                      0:04
                    
                    
                      except where I do variable annotations.
                      0:07
                    
                    
                      That's a Python 3.6-only feature, but
that, along with some other additions and
                      0:10
                    
                    
                      fixes, are a great reason
to consider updating.
                      0:14
                    
                    
                      So let's start with a simple calculator.
                      0:17
                    
                    
                      So we've got add, subtract,
multiply, divide.
                      0:19
                    
                    
                      And we're just kind of being naive, and
                      0:22
                    
                    
                      we're just assuming whatever gets
passed in can be added together.
                      0:24
                    
                    
                      Like I said,
this is a really simple example.
                      0:27
                    
                    
                      So right now the calculator will work
with many different Python types, right?
                      0:30
                    
                    
                      We could add two strings together or
multiply a string by an integer.
                      0:33
                    
                    
                      We could, of course, do all of these
operations with ints, floats, or
                      0:36
                    
                    
                      other number types.
                      0:39
                    
                    
                      However, I want to optimize this code in
the future for use only with number type.
                      0:40
                    
                    
                      Now I could rename the functions,
                      0:45
                    
                    
                      but no one wants to type in add ints or
add numbers.
                      0:47
                    
                    
                      This is where type inting comes in handy,
                      0:51
                    
                    
                      especially since I know my team all
uses PyCharm for their development.
                      0:52
                    
                    
                      I don't need to import anything or
do anything special.
                      0:56
                    
                    
                      I can just annotate the functions right
where they are for simple cases like this.
                      0:59
                    
                    
                      So for instance,
on def add I can say that num1 is an int.
                      1:03
                    
                    
                      And I can say that num2 is an int.
                      1:07
                    
                    
                      And I can say that this
function returns an int, right?
                      1:10
                    
                    
                      Because int plus an int
always returns an int.
                      1:15
                    
                    
                      And I can leave the rest of this alone.
                      1:18
                    
                    
                      So I'm gonna go ahead and
add this to the others.
                      1:20
                    
                    
                      So int, int, and int.
                      1:22
                    
                    
                      Int, int, Returns an int.
                      1:32
                    
                    
                      And lastly, int int.
                      1:38
                    
                    
                      This doesn't return an int,
this would return a float, right?
                      1:43
                    
                    
                      Because dividing returns a float.
                      1:48
                    
                    
                      So we can already see that PyCharm is
alerting me to something being wrong here,
                      1:51
                    
                    
                      right?
                      1:56
                    
                    
                      Expected type 'int', got 'float' instead.
                      1:57
                    
                    
                      And here, Expected type 'int',
got 'string' instead.
                      2:00
                    
                    
                      So while these will still work, right?
                      2:03
                    
                    
                      Python will still run these, and
                      2:06
                    
                    
                      this will still be a perfectly
valid fine thing, right?
                      2:07
                    
                    
                      We can go here, and we can run calculator,
and we got 13.2 and
                      2:11
                    
                    
                      hello hello hello hello hello.
                      2:15
                    
                    
                      PyCharm was telling me that I didn't
use these in the way that I said I was
                      2:17
                    
                    
                      going to use them.
                      2:21
                    
                    
                      So that's pretty nice, right?
                      2:22
                    
                    
                      After each parameter, just to go
over this again, I add a colon and
                      2:24
                    
                    
                      the type that I expect
the parameter to be, right?
                      2:27
                    
                    
                      And at the end of the function definition,
I can include an arrow,
                      2:30
                    
                    
                      made by a hyphen and
a greater-than symbol, and
                      2:34
                    
                    
                      then the type that's going to
be returned by the function.
                      2:35
                    
                    
                      If I had a function that didn't return
anything, I could change this last word,
                      2:37
                    
                    
                      instead of float, to be None.
                      2:41
                    
                    
                      And Python knows that that function
now doesn't return anything.
                      2:43
                    
                    
                      And it's similar to a void
function in other languages.
                      2:46
                    
                    
                      Okay, so we've seen the error messages,
we've seen the things there, so, cool.
                      2:50
                    
                    
                      I need to get a little smarter in
my types that I'm using, right?
                      2:55
                    
                    
                      Because this right here is
perfectly valid for add.
                      2:59
                    
                    
                      I can add an int and a float together.
                      3:02
                    
                    
                      Now I could use a generic to say
that this can be an int or a float.
                      3:04
                    
                    
                      But, well, that's for the next video,
and there's an easier solution, anyway.
                      3:09
                    
                    
                      Python has a built-in type that's called
complex that can encompass both integers
                      3:14
                    
                    
                      and floats.
                      3:18
                    
                    
                      But also allows for
a third type of number, a complex number,
                      3:19
                    
                    
                      which is a combination of a real number
and an optional imaginary number.
                      3:22
                    
                    
                      So let me try changing
some of those to that.
                      3:26
                    
                    
                      So let's try changing this to complex,
                      3:28
                    
                    
                      and complex, and complex.
                      3:33
                    
                    
                      Cuz I don't know exactly
what's gonna come out of that.
                      3:37
                    
                    
                      And that gets rid of the error
message about the 5 and 8.2.
                      3:39
                    
                    
                      So okay, that's cool.
                      3:42
                    
                    
                      Now if I wanted to make sure that this
was only used for real numbers, though,
                      3:45
                    
                    
                      and not complex numbers, I can bring in,
from numbers important real.
                      3:50
                    
                    
                      I can bring in the real type, and the real
type is just encompassing ints and floats.
                      3:54
                    
                    
                      And again, I just say this is gonna
be a Real, and a Real, and a Real.
                      4:01
                    
                    
                      And I would do the same for
the rest of all of these functions.
                      4:06
                    
                    
                      My code is error-free, with the exception
of this 'hello Which I can't fix because I
                      4:10
                    
                    
                      can't have something that encompasses
numbers and strings this way.
                      4:14
                    
                    
                      So if you're dealing with simple types,
adding this kind of hinting right here is
                      4:17
                    
                    
                      remarkably simple and
gains you some decent benefits.
                      4:22
                    
              
        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