An introduction to Python Type Hints - Deepstash

Bite-sized knowledge

to upgrade

your career

Ideas from books, articles & podcasts.

created 4 ideas

As I strive to become a better python programmer, I’ve started using type hints and created this intro guide

PYTHON

An introduction to Python Type Hints

An introduction to Python Type Hints

peps.python.org

STASHED IN:

48 reads

Type Annotations

PEP 484 introduced type hints (typing module), a.k.a type annotations. While its main focus was function annotations, it also introduced the notion of type comments to annotate variables.

Ex: primes = [] #type...

Variable Annotations

PEP 526 introduced the syntax for Variable Annotations:

var: annotation = value

Variable Annotations don’t affect the Python principle of being a dynamically typed language.

Type annotations can’t create errors, they are checked only by third party type checkers...

Class and Instance Variable Annotations

Type annotations can also be used to annotate class and instance variables in class bodies and methods.

In particular, the value-less notation: a: int allows one to annotate instance variables that should be initialized in __init__ or __new__. See the syntax in the picture.

A...

Class Variables vs. Instance Variables

When one defines a member variable in the class body definition, the interpreter considers it a class variable.

class Dog:

name: str = ‘Doggo’ # class variable

In the __init__ constructor, one does have access at the ‘name’ class variable using ‘self’. But...

1 Reaction

Comment

MORE LIKE THIS

created 6 ideas

I started using Dataclasses and I think you should too

4

Comment

99 reads

created 3 ideas

Goal: To provide a simple way to get up and running a high-performance HTTP server that is easy to build, expand, and ultimately to scale.

React

Comment

126 reads

It's time to

READ

LIKE

A PRO!

Jump-start your

reading habits

, gather your

knowledge

,

remember what you read

and stay ahead of the crowd!

Takes just 5 minutes a day.


TRY THE DEEPSTASH APP

+2M Installs

4.7 App Score