Why I Always Do This In Python

preview_player
Показать описание
This channel has grown big through the past couple of years, and one of the most frequent comments I get is: "why do you annotate everything in Python?". I've also been told that Python isn't Java (thank you so much I didn't realise they weren't the same). Anyway, in today's video I just wanted to share with you why I use type annotations everywhere!

▶ Become job-ready with Python:

▶ Follow me on Instagram:
Рекомендации по теме
Комментарии
Автор

Plus it makes the linter of whichever IDE you're using more capable

HitBoxMaster
Автор

I think type annotation is really a cool thing. It helps you write more explicit and readable code

konstantinzakharov
Автор

In many languages we are always required to explicit the type. Python tutorials invite people to not do that, which in serious projects it's a very bad idea.
I like how in dart you can just define the type as "var", that means the type is only one and won't change, but it gets inherited by the value you assign to it. (It's not the "Any" type, that is called "dynamic" there).
Wish more languages had that.

intron
Автор

I have been trying so hard to understand the importance because it gets confusing for me now that the standards have been updated so much since my last go at programming, but at least this now gives me explicit reasons why it's essentially necessary and not just a frill.

Ascendance
Автор

I like type annotations because it marks the error where has actually been made, not buried 5 layers deep in a random library when a type conversion fails, because some libraries don't (or poorly) annotate function arguments.

Pasu
Автор

It doesn’t matter who says what, don’t worry about anyone who came from another profession like me, when someone who uses the computer only to play games, I believed when he said that python was easy when he tried to learn the programming language, but this was not true because the equivalents of the types were uncertain. I think that people who are just starting to learn with this method will learn much more comfortably and will fit the concepts more correctly. After learning over time, they can leave the writing style that is seen as too much. But what you described is completely true.

pointcodding
Автор

When first looking at your videos, I was confused, but now I love it. I don't use it all the time, only with the input of functions

storvattsuisse
Автор

I use annotations mostly like I use them in the language that most of the syntax for python's type annotations come from. That is TypeScript.

Intellisense for Python also uses mostly the same logic, so you always get autocomplete and type hints on hover.

Which means, I mostly don't annotate types, doing so only in places where it can't be inferred, such as function arguments.

There's a few differences. Inference of the return argument works fine in TS, in Python that often fails, so I annotate return types in Python. I will also manually annotate assignments when return types of libraries are incomplete or too generic, which is still happening fairly frequently.

SMTM
Автор

Whenever I hear an engineer say something is "obvious" I know I'm dealing with a junior person, After 35 years as a software engineer, there is no such thing as obvious. What is obvious to you today may well be obscure to you in the next 6 months because your head is in a totally different space, Further what is obvious to one person may not be to another. Being clear and explicit is always trhe best path n engineering! 😃

jeffdavies
Автор

“…some might not care at all if you're creating some throwaway scripts you might not care about all
this safety because you won't use this script ever again…”. Unfortunately, many “throwaway” scripts/programs can take on a life of their own.

bevintx
Автор

In big serious and multi-dev projects I believe it's a must.

For most cases thou, and 100% of my cases (I use Python everyday for work and also hobby, I've been programming for almost 4 decades) I never do type annotations. Unnecessary added complexity
when you know what you are doing. BUT as I said, sometimes the code is touched by many and it helps a bit. But I do prefer the original idea over Python.

smanzoli
Автор

I think typing is useful for function/method arguments and their outputs. It lets the the IDE, the user and other explicit know what to expect. But when using it when initializing the variable is basically redundant.

rvons
Автор

I wish i started learning with Java or JS rather than Python.

hassani
Автор

If my code is going to be used by someone else or The editor I use (Visual Studio Code) leaves it white because it can't figure out what type it is (it also can't guess what I'm going to write), so I use it.

bartuustaoglu
Автор

Not only. Without explicite type you can override type later without warning (`a = 42; a = 'the Answer'`).

robertkalinowski
Автор

Problem I am seeing with type hints now is that I am starting to see code like this.

number: int
number = 10

But I do see the usefulness and I think it'll be an advantage for type hints going forward because we will see things like Mojo that can take advantage of your type hints and improve your code performance.

gardnmi
Автор

First to watch and comment on a YouTube video that's a first, great content . I actually also just changed IDE typing checking to strict. Pushing me to type annotate always .

TheimmortalsimpGod
Автор

i just anotate when the language server doesn't know what type is

agatemosu
Автор

type annotation is good for big project, but you still can change its type after assigning some value to the same variable.Furthermore, the annotation can not improve the program performance.

for me, I prefer to use ctype, numpy and numba for explicit type in python.

BohonChina
Автор

I've become lazy enough with types in python that I start using type hinting and end up forgetting about it by the end of writing the class 😢

marcomoscoso