

Python type annotations how to#
They’re usually one of the first things you learn when you learn how to program. What kind of data? That’s dicated by the language’s choice of data types.Įvery language has data types. How does the hardware know how to store those 0s and 1s in memory? The software, our code, needs to tell it how to allocate memory for that data. The latter is how Python (and PHP, Ruby,and similar “scripting” languages) works.
Python type annotations code#
There are a couple ways to translate your code into machine-legible code: you can either build a binary and have a compiler translate it (C++, Go, Rust, etc.), or run the code directly and have the interpreter do it. When languages are either compiled or executed (python is executed via interpreter), the code is turned into lower-level machine code that tells the lower-level components of the computer i.e. This is where compiled and interpreted languages come in. To get to those 1s and 0s, we need to move from our high-level, to low-level language. A representation of machine language is 1s and 0s. It can do really powerful stuff, but it only understands machine language, which, at its core, is electricity.


Programming languages, at their core, are a way of doing things to data using the CPU, and storing both the input and output in memory. To understand what the Python core developers are trying to do here with type hints, let’s go down a couple levels from Python, and get a better understanding of how computers and programming languages work in general. So, like I usually do with things I don’t understand, I decided to dig in further, and am hopeful that this post will be just as helpful for others.Īs usual, if you see something and want to comment, feel free to submit a pull request. When I started to investigate and weigh whether type hints made sense for me to use, I became super confused. However, there’s still a lot of confusion around what they are (and what they’re even called - are they hints or annotations? For the sake of this article, I’ll call them hints), and how they can benefit your code base. Def function(variable: input_type) -> return_type:
