Python: A Quick Guide To Type Annotations (ft. Mypy)

preview_player
Показать описание
In today’s video we will be learning about type annotations in Python, and also why I always try to type everything with no exceptions.

▶ Become job-ready with Python:

▶ Follow me on Instagram:

00:00 Learning Python made simple
00:05 Intro
00:32 Getting started
03:35 Mypy
05:12 More benefits
08:26 Isn't it redundant?
11:10 Your thoughts
Рекомендации по теме
Комментарии
Автор

I apologise about the background static noise (if you're using headphones you'll notice it). My microphone fell apart earlier this week and I had to order some replacement parts, and as usual, anytime I even slightly modify the setup of my mic on my desk, it retaliates xD I'm fixing it now so it should be fine in the next video :)

Indently
Автор

I like that Python provides this stuff but doesn't insist on it. If I'm knocking up a quick script to do something once then it's unnecessary, but in bigger codebases or with multiple maintainers this can avoid a lot of problems.

logaspam
Автор

When I started using some of your tips and tricks I have vastly improved my coding skills and that was also acknowledged by my senior colleagues. I now do it rarely without annotations and other stuff I have seen on your videos. Thanks for advancing my skills here ;)

wootdafuq
Автор

First I thought you had another collab, then I realized mypy wasn't a person 😅😂

ultimateownsz
Автор

This is so clear and well structured! Thank you. This is how all languages should be taught!

qwarlockz
Автор

For me, I use annotations all the time now. And I do it because it helps me think beforehand what the function is about. And it helps me afterwards, finding what I was thinking, too.

christophhenninger
Автор

The situation i find type annotations the most useful is using it in function signitures, it gives you type hints within the function body. This is especially good if some arguements are objects with complex class methods (usually provided by third party libraries).

Btw it also coerces input arguements to the type you specify so you save some effort in dealing with edge cases

Shivvorz
Автор

maybe you didn't mean it, but another argument in explicitly specifying the type for a variable may be the desire to indicate that this is its declaration, and not a re-assignment :)

unlomtrash
Автор

Type annotations were at first annoying for me, but once I realized they prevent me from making mistakes and give me different cases I might not have thought of, I’ve found them very helpful.

Nerdimo
Автор

I just solved a problem for my company using type annotations. Literally the tool I used to solve the issue

CASnumber
Автор

Excellent as ever in all your precious videos! Thanks for your work. Please keep continue!

Diegos
Автор

Thanks. I recently pondered this question myself (typing obvious assignments). It's good to see you came to this conclusion.

markchadwick
Автор

Good video, would just like to point out that some code editors are better than others in recognising type errors. For example, in VS Code using the example you provided
elements: list[str] = [1, 2, 'a']
it highlights the list and gives the following error:
"Expression of type "list[int | str]" cannot be assigned to declared type "list[str]"
"Literal[1]" is incompatible with "str"
"Literal[2]" is incompatible with "str"

I do still use mypy to check my files though :) and I also use type annotations for everything.

rethanon
Автор

Just for me personally, I prefer where the language itself mandates that all variables are explicitly typed. I like Python, and feel this type of video is valuable about how typing can be helpful.

johnmckown
Автор

Typed code is easier to read, thus easier to maintain and less buggy. The additional few seconds here and there pale in comparison to the time that can be saved due to those benefits. One additional bug due the wrong type being used can make up for the trivial amount of time it takes to include types on a whole lot of code. Personally I wish it was strongly typed and the Python compiler did care, but at least having type support and warnings in the IDE is better than nothing.
P.S. Really good senior professional developers understand that considerations like readability, maintainability and consistency make for better code that saves significant time long-term. Only specifying types in some cases or for some projects makes your code inconsistent. Consistency has significant benefits.

Me__Myself__and__I
Автор

Man. you're writing very clean and smooth code. I get used to care these annotations by you. Many thanks.

loverboykimi
Автор

Hi, many thanks for this video, I again learnt something and will definitelly follow. Even your "fruit: Fruit = Fruit()" is more than consistency. Se my example:

dish1: Fruit = Fruit()
dish2: Vegetable = Vegetable()
dish3: Vegetable = Vegetable()
dish4: Vegetable = dish2
dish5: Vegetable = dish1 # This will display a type checking error

ghrasko
Автор

The type annotations for function params and return types is most useful to me and I use the inline ones like fruit: Fruit = Fruit() for my own classes like that. I guess the inline ones are most useful for more involved types like ones you made or something like list[str]. It gives you autofill if you do that which is nice. I don’t see the benefit of doing simple types like x: int = 11 tho.

darcash
Автор

Regarding your section on the redundancy, I feel that it is only redundant because it's such a simple example. Instead, say you have some child classes to the class "Fruit"... Then this starts to make a lot more sense! Typing your variable as "Fruit" then shows that you can assign any of the child classes (e.g. Banana(), Apple(), etc.) to it while still maintaining functionality of the code. This also implies that you should only be using methods in the parent class Fruit when using that variable.

marcelpumpernickel
Автор

I have been using type annotations for a while now. It's really helpful. Great explanation. It's easy to debug and read as well.

I have a request, can you make videos on JSON and Object serialisation in python objects

tharunkanna