An introduction to Python Type Hints - Deepstash
An introduction to Python Type Hints

An introduction to Python Type Hints

Curated from: peps.python.org

Ideas, facts & insights covering these topics:

4 ideas

·

153 reads

3

Explore the World's Best Ideas

Join today and uncover 100+ curated journeys from 50+ topics. Unlock access to our mobile app with extensive features.

Type Annotations

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: List[int]

Although type comments work well enough, there are some downsides:

  • text editors highlight comments differently
  • one can’t annotate uninitialized variables
  • mixing type comments with normal ones is confusing

The solution: Variable Annotations

10

59 reads

Variable Annotations

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 like mypy.

See image example.

10

38 reads

Class and Instance Variable Annotations

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.

As one knows, defining class member variables is not mandatory but it’s useful for providing autocomplete functionality while coding.

The differences between class and instance variables are discussed in the following idea

10

26 reads

Class Variables vs. Instance Variables

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 as self represents the instance of the class, in the constructor scope, ‘self.name’ becomes an instance variable.

By deleting the ‘name’ attribute from a Dog instance, one can access the ‘name’ class variable once again.

10

30 reads

IDEAS CURATED BY

pvl

Ex Backend @deepstash

CURATOR'S NOTE

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

Pavel-Vlad Mateescu's ideas are part of this journey:

Machine Learning With Google

Learn more about computerscience with this collection

Understanding machine learning models

Improving data analysis and decision-making

How Google uses logic in machine learning

Related collections

Read & Learn

20x Faster

without
deepstash

with
deepstash

with

deepstash

Personalized microlearning

100+ Learning Journeys

Access to 200,000+ ideas

Access to the mobile app

Unlimited idea saving

Unlimited history

Unlimited listening to ideas

Downloading & offline access

Supercharge your mind with one idea per day

Enter your email and spend 1 minute every day to learn something new.

Email

I agree to receive email updates