Use Static Typing in Python 3.6

Will Button
InstructorWill Button
Share this video with your friends

Social Share Links

Send Tweet
Published 6 years ago
Updated 5 years ago

In this lesson, you will learn how to statically type variables in Python 3.6 Static typing can help you avoid errors when consumers of your variables and functions call them with the incorrect data type. You will learn how to statically type strings, integers, booleans, Lists and Dicts. You will also learn how to automatically check for typing errors when coding and as part of your build workflow.

Instructor: [00:01] If you've ever had one of those times in Python where you create a function and then someone else comes along and they try to use your function, even though you tested it they come back to you saying that your function isn't working.

[00:14] After a lot of conversations back and forth and troubleshooting, you find out that they did something like this where they were supposed to use an integer and instead supplied a string. Now in Python 3.6 you can do static typing and prevent some of those errors from occurring.

[00:30] To static we type a variable in Python. You declare your variable like you always would and then add a colon and the type of variable that it is. In this case we want A and B to be integers so we can specify it as A:int and B: int.

[00:46] Now because I'm using Pycharm it's going to do the automatic type checking and then highlight the variable Fu here and says that it's expecting a type integer but got a string instead. We know right away that that's not going to work.

[01:02] If you're not using Pycharm as your Python editor you're not out of luck because you can use Mypy to perform the same thing. You can find all the documentation from Mypy on their website at Mypy-lang-org and you can use pip to install it on your own system.

[01:18] Then you can use Mypy by supplying the Mypy command and the name of the file you want to check. When it fails it gives you the line number that it failed on as well as the error that caused it to fail. This is cool because you can integrate this with your CI or CD solution and help identify these errors at build time rather than waiting for them to be discovered in production.

[01:45] In addition to declaring integers you can declare string variables, Booleans. By importing from the typing module, dict enlist, you can declare a list with the data type that should be in the list. You can do the same for dict to specify what the structure of the dicts should look like.

egghead
egghead
~ an hour ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today