• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
cropped CodeSpecialist e1592127475203

Code Specialist

Code and Programming Blog

  • Home
  • Clean Code
  • Code Principles
  • Code Interview
  • Python

Use Python Type Hints!

Python differs from other programming languages in many ways, one of them is dynamic typing. You can't specify explicit types for variables, functions, or any other object. However, since the missing type declarations can be confusing and misleading, Python 3.5 introduced Type Hints!

Home » Python » Use Python Type Hints!

August 17, 2020 by Jonas Scholl Leave a Comment

Contents hide
1 What are Python Type Hints?
2 Types in Python
3 How do Type Hints look like?
4 Benefits of specifying types

What are Python Type Hints?

Type Hints in Python specify the type of variables or functions parameters as well as the return type. You can read more about them in the PEP 484 Guidelines.

In general they work like type declarations in every typed programming language, like Java or C++. The only difference is that Type Hints are completely optional. Python remains a dynamic language, because the Interpreter simply ignores the Type Hints. They are only relevant for development and have no influence on the execution. Accordingly, you can run a Python program with completely incorrect Type Hints without getting any errors.

Type Hints exist just for you as a developer to make your life easier – so you should use them!

Types in Python

Python has many built-in types, way more than you might have expected. Some common examples for build-in types are str for strings, int for integers and bool for boolean values.

Furthermore, types are objects like everything else in Python. By the way, you can actually use types as keys for a dictionary. So the following code really works!

Okay, you can do lots of things with the build-in types, but they are not really specific. For instance, you can have a variable of the type list, but what’s actually in the list? Nobody knows.

That’s why Python 3.5 introduced the typing module! You can import various types from this module, which you can specify really precisely. The following examples show some features of the typing module, but feel free to take some time and explore the module on your own.

How do Type Hints look like?

The syntax for Type Hints is, like the Python syntax in general, very simple.

In the following code example, you can see some type specifications for variables. The according syntax is just a colon after the variable name followed by the type.

For these examples the types might seem a bit redundant and obvious. In fact, you will use this kind of Type Hint rather seldom in comparison to the others. But when you use the returned object of a function for instance, you may not know the type of it.

More frequently used are Type Hints for parameters, and they basically work just the same as for variables. But note that this applies for parameters with default arguments too!

Another possibility to specify Type Hints is for the return type of a function. There is also another syntax for this case: You specify the return type with an “arrow”, followed by the type. It is important that the type is in the function definition before the colon.

This is the complete syntax you need to know if you want to use Type Hints! Easy, isn’t it?

Our recommendation to get more familiar with Python: (Affiliate)

Benefits of specifying types

Some IDEs like PyCharm support Type Hints in different ways. First of all, PyCharm complains when a Type Hint does not matches with your coding. Additionally, you get a suggestion what the type should be or what is expected. This prevents unintentional behaviours where you expect a type and are surprised when you get something completely different. With Type Hints, you don’t have to reengineer every function you want to call just for knowing what it will return anymore. In most cases, the combination of the function name and the type specifications make it clear how to use the function. If it doesn’t, you should improve the readability of your code, for example by applying the KISS Principle or giving Meaningful Names.

Another really useful feature is the auto-completion! A common use-case for this scenario is an instance object from a class with some attributes and methods.

If you call a function which returns an object, you might store it in a variable and work with it. Without Type Hints, the IDE wouldn’t know what alice is. If you type a point after the name, you won’t get any useful suggestions for functions or attributes of the Dog class. This changes when you add Type Hints!

Now you will get all possible suggestions for working with the class instance.

As we have seen, Type Hints are a really powerful tool to increase your productivity and motivation. It also greatly improves the readability of your code as everyone can quickly see what kind of objects you are working with. So all in all, you should definitely use Type Hints!

Download Source Files
jonas scholl
Jonas Scholl

There is nothing that excites me more than solving problems and understanding complex concepts. I want to share this enthusiasm by making complex things as simple as possible, so everyone can understand them. Nothing is more frustrating than trying to understand a topic with incomprehensible explanations. That’s why I want to inspire you to become a better developer by simplifying concepts and explaining issues clearly.

Our Book Recommendations on Python (Affiliate)

Category iconPython,  Write Better Code Tag iconReadability

Related Posts

hands on writing machine
Meaningful Names
July 31, 2020
coding on a laptop
Comments – What you should keep in mind
July 22, 2020
do not hear see speak frog
Do one thing – Not the single responsibility principle (SRP)
July 20, 2020

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published.

Primary Sidebar

Search

Categories

  • Algorithms
  • Cloud
  • Code Interview
  • Code Principles
  • Environment
  • Errors
  • Hardware
  • Learn to Code
  • Nice to know
  • Object Orientation
  • Python
  • Technical Background
  • Write Better Code

Tags

Alternative (1) Announcement (1) Clean Code (7) Cloud Foundry (1) Cloud Native (1) Code (1) Code Interview Question (2) Code Philosophy (1) Comments (1) Computer Graphics (2) Concurrency (1) Control Characters (1) Data Science (1) Data Structures (1) decorators (2) dictionaries (3) Easter Eggs (1) funny (1) Github Actions (1) Marching Cubes (1) Meshing (1) Microservice (1) Nice to know (1) Parallelisation (1) Programming Facts (1) Pythonic (3) PyYAML (1) Readability (4) Simple is good (2) Software Engineering (2) switch (1) Syntax (1) Tricks (1) type hints (1) Ubuntu 20.04 (1) Underscores (1) Unix (1) Video (1) Virtualization with Windows (1)

Footer

Recent Posts

  • Cloud Foundry Components
  • No matching distribution found for pkg-resources==0.0.0
  • SOLID Principles with Python Code Examples
  • 5 Hardware Ideas to Upgrade your Programming Experience
  • 5 Programming facts every programmer must know

Tags

Alternative Announcement Clean Code Cloud Foundry Cloud Native Code Code Interview Question Code Philosophy Comments Computer Graphics Concurrency Control Characters Data Science Data Structures decorators dictionaries Easter Eggs funny Github Actions Marching Cubes Meshing Microservice Nice to know Parallelisation Programming Facts Pythonic PyYAML Readability Simple is good Software Engineering switch Syntax Tricks type hints Ubuntu 20.04 Underscores Unix Video Virtualization with Windows

Categories

  • Algorithms
  • Cloud
  • Code Interview
  • Code Principles
  • Environment
  • Errors
  • Hardware
  • Learn to Code
  • Nice to know
  • Object Orientation
  • Python
  • Technical Background
  • Write Better Code

Recent Comments

  • Nina on Bubble Sort in Python and how to Visualize it
  • Bhan on Concurrency in Python

Follow Us

  • Facebook
  • Instagram
  • Pinterest
  • Twitter

Quicklinks

  • About us
  • Become a writer
  • Guest Post

Privacy Policy | Legal Notice | Contact

 

All rights reserved © 2020-2021 code-specialist.com