The BIGGEST Misconception About Type Hints In Python Explained

  Рет қаралды 57,481

Indently

Indently

Күн бұрын

Пікірлер: 123
@lucky13pjn
@lucky13pjn 2 жыл бұрын
For those of us coming from strongly typed languages, type hinting is a nice little security blanket.
@evlezzz
@evlezzz 2 жыл бұрын
Oh, that's actually another common misconception with terminology. Python IS a strongly typed language. People tend to mix two separate ways of classification, which are Strong/weak vs static dynamic. Python has a strong dynamic typing.
@ZeroSleap
@ZeroSleap 2 жыл бұрын
@@evlezzz Oh interesting. So what the commenter ment is more the Static Typing part?
@mattizzle81
@mattizzle81 2 жыл бұрын
I mostly care about intellisense. Sometimes I even create “dummy” modules to import just with type information to allow intellisense to work.
@0DAYBROKER
@0DAYBROKER 2 жыл бұрын
@@mattizzle81 a great way to import "dummy" modules just for type hinting is to do the following: from typing import TYPE_CHECKING if TYPE_CHECKING: from dummy.module import DummyType now it will only import "DummyType" when intellisense is type checking.
@0DAYBROKER
@0DAYBROKER 2 жыл бұрын
this can also help prevent import cycle errors
@r3d646
@r3d646 2 жыл бұрын
Personally I only use type hints in function definitions because it helps with documentation. Not having to do that for every variable is like half the reason I’m using python in the first place 😅
@PanduPoluan
@PanduPoluan 2 жыл бұрын
Most of the times good IDEs can infer what a variable is supposed to contain. So I agree But some hairy functions with complex innards, I'd sparingly add some type annotations to maintain my sanity
@esphilee
@esphilee 10 ай бұрын
True. Python is like a beach that topless is allowed. Some people just don’t feel secure without it.
@JSDudeca
@JSDudeca Жыл бұрын
It really helps with auto-completion when dealing with custom modules and classes. I have been leaning heavily on this of late.
@ToveriJuri
@ToveriJuri 3 ай бұрын
Helps with code completion and helps you keep track of things easier. Type hinted code is so much easier to return to if you've been away from it a little bit. I've gotten used to just doing it even for small scripts because I find it useful.
@caiolaytynher5994
@caiolaytynher5994 2 жыл бұрын
One thing that I do since type hints are ment to improve readability and IDE workflow is only using them if the type is not explicit, either for the user or the IDE, like return types after calling a function. This means that, for me, creating variables using the primitive data types or using the class constructor don't really need type hinting because it's obvious. For example: banana = Fruit() In this case, both the IDE and the user know that banana is a Fruit, so adding annotations wouldn't do anything or make your code just more verbose. That's my opinion though, what you guys think about it?
@Indently
@Indently 2 жыл бұрын
I think it’s a respectable view on it. And I think most people will probably also disagree with my: banana: Fruit = Fruit() But that’s now a huge habit of mine. Even if it looks redundant, I enjoy having it because it feels even extra explicit in my mind. I think what you said makes sense though!
@schwingedeshaehers
@schwingedeshaehers 10 ай бұрын
the problem is when you reuse a variable, without knowing. the changed type can give you a warning
@b4ttlemast0r
@b4ttlemast0r 7 ай бұрын
Even in C++ I'd write "auto banana = Fruit()" in that case and not write the type name twice.
@GeorgeNoiseless
@GeorgeNoiseless 2 жыл бұрын
And it's a good habit to have for when you have to learn\switch to strongly typed language. Learning something like TypeScript or Dart is so much easier if you practice sound typing in Python.
@AlFasGD
@AlFasGD 2 жыл бұрын
All developers that have been writing dynamic code for years have been snorting that copium, Python couldn't last too long without type hints, proving once again that dynamic programming is not to be worshipped. It's nice to have every now and then, but definitely not for full systems, without any sort of type information. Same reason why we also got TS on top of JS
@thomasricatte8287
@thomasricatte8287 2 жыл бұрын
Thanks for the video! In my case, I am using type annotations super often (at least in all function definitions) + mark type hint warning as error in the IDE ; it saved a terrific number of hours (not to mention forcing myself to improve my interfaces).
@bozok1903
@bozok1903 2 жыл бұрын
I like using type hints and return type in function definition. It makes reading the code , writing documentation and debugging easier.
@KetilK
@KetilK 2 жыл бұрын
I sometimes wish there was a strict python debug mode that automatically asserted that your objects in fact conformed to your type hints at runtime. In my opinion, debugging python code in larger projects can be hard at times.
@preritdas6998
@preritdas6998 2 жыл бұрын
Try a static type checker like mypy
@itsmeben604
@itsmeben604 2 жыл бұрын
It's not quite what you're talking about, but check out pydantic. It allows you to create data models and allows some type enforcement.
@evlezzz
@evlezzz 2 жыл бұрын
Look at mypy if that's what you are looking for. I would say though that enforcing restrictions on everything is not very useful and could easily limit your options and make your code worse, since it takes a lot of effort to annotate everything correctly, maintain that and not put unnecessary restrictions that prevents you from reusing your code. Personally I tend to annotate public interfaces as much as it looks reasonable, but not internals. I found that if your code is structured well enough, you normally should be able to comprehend what's going on in short functions without spending time on maintaining type hints for them. Also keep in mind that linters could use not just explicit annotations, but could also infer possible type from the code itself. So if you write something like. x = 1 if cond else "1", then linter after consuming this line would infer that x could be an int or a str and use this information later.
@JaiminBrahmbhatt
@JaiminBrahmbhatt 2 жыл бұрын
should try pyre-checker
@DaryaIbrahim
@DaryaIbrahim 2 жыл бұрын
Your voice is sooooo relaxing man, love to hear you teach forever, please give some advanced topics as well, not all of us are beginners
@Indently
@Indently 2 жыл бұрын
I will get to them next year, any recommendations for topics you'd like to see?
@DaryaIbrahim
@DaryaIbrahim 2 жыл бұрын
@@Indently Well, map() function in both JS & Python is my instance problem, even though it should be so simple, I just need to convert a huge list with 3 to 4 levels deep nested lists, only need a list from only one element. Also, please don't forget about the banana, he is our favorite friend 🍌
@Indently
@Indently 2 жыл бұрын
I will keep it in mind! I never forget about banana 😉
@allo5668
@allo5668 2 жыл бұрын
3:11 Guido has hinted that there are no current plans to use type hints to speed up the interpreter, but that it’s something they’re going to look into down the road (5, 10 years time). So it’s not crazy to think that the hint could speed up the code when it’s running one day
@Indently
@Indently 2 жыл бұрын
Typython
@adiveler
@adiveler Жыл бұрын
It's also great adapting this habit if you are learning non-dynamic programming languages!
@DrGreenGiant
@DrGreenGiant 10 ай бұрын
Worth mentioning that I've found type hinting plays very nicely with tools such as GitHub Copilot. This massively speeds up development. I do wonder if cPython or pypy interpreters, for example, have any optimisations, or might get some in the future, where type hints improve JIT compilation speeds.
@JT-mr3db
@JT-mr3db Жыл бұрын
Type hints are wonderful for documentation, absolutely love this.
@porzo2964
@porzo2964 2 жыл бұрын
had a good laugh at your int initialiser reaction :D:D
@fluffy-cat
@fluffy-cat 2 жыл бұрын
2:30 this will cause an error in python 3.8, and not just for typings, it will break the whole code, and the fix is: from typing import List and use it with capital L. Same with Dict and Tuple. Backwards compatibility makes life easier for people with low storage space / slow internet speed that already have dependencies installed with a lower python version.
@josephgaviota
@josephgaviota 10 ай бұрын
I currently write in what I call "Python 2+" ... working on a system with both py2 and py3, I write in such a way, the code will work in either. (2.7 and 3.11)
@gerardonavarro3400
@gerardonavarro3400 6 ай бұрын
​@@josephgaviotabut that's just... old python lol
@josephgaviota
@josephgaviota 6 ай бұрын
@@gerardonavarro3400 Yes, but one has to work with what one has.
@rahevar3626
@rahevar3626 2 жыл бұрын
At 2:42 why was there a pi and beta when you tried to write list[str] is that a option i can turn on
@Indently
@Indently 2 жыл бұрын
That's me miss-typing on my keyboard actually ahahaha
@chri-k
@chri-k 2 жыл бұрын
@@Indently i also keep typing greek everywhere by accident. my keyboard layout switching shortcut is a bit too convenient.
@Indently
@Indently 2 жыл бұрын
@@chri-k European keyboards are a pain for coding ahahah.
@Andrumen01
@Andrumen01 Жыл бұрын
The "typing" module includes broader and slightly better options for "typehints".
@leleemagnu6831
@leleemagnu6831 2 жыл бұрын
Great video, truly loved it! More on the subject would be nice!
@LegenDUS2
@LegenDUS2 2 жыл бұрын
Explicit is better than implicit - Zen of python
@PanduPoluan
@PanduPoluan 2 жыл бұрын
Ahahaha the bleep, lol 😂 I love type hinting. It makes IDEs much more useful. And also makes me more disciplined in writing my code. Next video suggestion if you haven't: The amazing power of "for unpacking" 😎
@Indently
@Indently 2 жыл бұрын
Thanks for the suggestion!
@nilshamacher5064
@nilshamacher5064 2 жыл бұрын
I also like those typehints, and I think you missed that they're helping also for overloading functions if I'm correct. Coming from C++ , I appreciate types anyway 😉
@Indently
@Indently 2 жыл бұрын
Python doesn’t support overloading as far as I remember
@evlezzz
@evlezzz 2 жыл бұрын
@@Indently Not exactly. Overload is possible via functools.singledispatch decorator and it uses typehints to distinguish, which implementation to call. Typehints are not enforced in any way by language itself (unless you use something like mypy instead of regular cpython to run scripts, but that's a different story). However nothing prevents your code from reading function annotations and do some magic around them. That's exactly what singledispatch does, so it's rather a syntax sugar than a feature of language itself.
@MingwaiTam
@MingwaiTam 11 ай бұрын
I hope the type hints will be used to facilitate the just-in-time compilation. It looks so reasonable.
@castlecodersltd
@castlecodersltd 2 жыл бұрын
Very interesting. Almost as interesting as Copenhagen, where I was over the weekend. Thank you. ☺
@georgioszampoukis1966
@georgioszampoukis1966 2 жыл бұрын
I think one instance where it does check the hints is when cythonizing to produce .pyd files.
@mahdirasouli6005
@mahdirasouli6005 Жыл бұрын
Did you worked with mypyc?
@lukaschumchal7797
@lukaschumchal7797 2 жыл бұрын
You made my day. Thanks 🙏
@judithlee7989
@judithlee7989 2 жыл бұрын
Even though vim does not give me any warning if I'm assigning values against the type hints, I find it very useful and handy when I'm tracing some code.
@josephgaviota
@josephgaviota 10 ай бұрын
Thank you for this video. I've seen these type hints in various videos, and wasn't sure what's the purpose, since in my tests (by copying things in vids) ... it seems to have no effect. And since I've been writing code in vi for 40+ years, these type hints don't seem to have any benefit to me. BUT, I _DO_ believe in good commenting, good variable names, and using plenty of """ Very explanatory doc strings with examples including Use Cases """ so I hope programmers that follow me, will not be mad at me.
@vhmolinar
@vhmolinar 2 жыл бұрын
Is it possible to tell the compiler to consider these type definitions?
@md.redwanhossain8822
@md.redwanhossain8822 2 жыл бұрын
There is no compiler in python, it has an interpreter.
@AmansLab
@AmansLab 2 жыл бұрын
In Future releases of python this will be used in runtime. so, it is good practice
@Indently
@Indently 2 жыл бұрын
Do you know where I can find the docs for this?
@preritdas6998
@preritdas6998 2 жыл бұрын
Guido, Python creator, and other maintainers have said there are no plans for this as it goes against pythons core values.
@Indently
@Indently 2 жыл бұрын
That's what I thought as well, but maybe Aman has some article that we don't know about that he is willing to share with us.
@preritdas6998
@preritdas6998 2 жыл бұрын
@@Indently have you heard Guido on the lex Fridman podcast? If not, highly recommend. He was on twice, the most recent one was stellar.
@Indently
@Indently 2 жыл бұрын
Thanks for the recommendation! I've never heard of it no, I will check it out though.
@calebparks8318
@calebparks8318 Жыл бұрын
Mypy has a compiler that should speed-up your code. I believe that type-hints are required.
@qondonyon
@qondonyon 2 жыл бұрын
yep
@mikeg9b
@mikeg9b 10 ай бұрын
3:17 "...no type checking is occurring at run time." That doesn't sound right. Python always does type checking at run time, and type hints doesn't change that (because Python ignores them). That's one of the reasons Python is slow. On the other hand, in languages like Go and Rust, BECAUSE the type checking happens at compile time, there is no need for it at run time.
@hatdidog864
@hatdidog864 2 жыл бұрын
can you make a video about the docstring, sir?
@lukekurlandski7653
@lukekurlandski7653 2 жыл бұрын
Do people out there actually think that type annotations "speed up" your code? Seems like you'd actively have to avoid reading any documentation to have this misconception.
@Indently
@Indently 2 жыл бұрын
If you every used a language that is statically typed before, it's very easy to make this mistake in Python if you're new to it.
@RedHair651
@RedHair651 10 ай бұрын
I thought that
@alidev425
@alidev425 2 жыл бұрын
thanks bro for your clarification'👍👍 ,btw what is your favorite IDE for python programming?
@Indently
@Indently 2 жыл бұрын
I love PyCharm
@airatvaliullin8420
@airatvaliullin8420 Жыл бұрын
You can also run typecheckers
@Neckhawker
@Neckhawker 3 ай бұрын
Type hints DOES speed up execution on some Python interpreter (Cython IIRC ?).
@Indently
@Indently 3 ай бұрын
True, but that requires extensive setup.
@uplink-on-yt
@uplink-on-yt 2 жыл бұрын
Ah, it doesn't do what PHP 8 does, yet. It doesn't care about the type hints at compile time. PHP does generate more efficient bytecode when you use type annotations, and does do type checking - it's still opt-in, but it's now standard usage.
@Indently
@Indently 2 жыл бұрын
Do you mean PEP? Or is there something with PHP that I’m missing?
@uplink-on-yt
@uplink-on-yt 2 жыл бұрын
@@Indently PHP, the programming language, competitor to Python on the web, but not so much on data crunching
@Indently
@Indently 2 жыл бұрын
Excuse my ignorance, I'm not really familiar with PHP so I thought you meant PEP ahah.
@uplink-on-yt
@uplink-on-yt 2 жыл бұрын
@@Indently No worries. I figured as much. I don't know much more than what I shared about PHP bytecode generation anyway. I figure Python will get there too at some point.
@insidious6068
@insidious6068 2 жыл бұрын
Does this only work in PyCharm or can this work in something like VS Code?
@RedHair651
@RedHair651 10 ай бұрын
It's part of Python, you can do it everywhere
@Knud451
@Knud451 Жыл бұрын
Thanks! Maybe a n00b question, can you also specify the parameter and return type to be e.g. a dataframe?
@schwingedeshaehers
@schwingedeshaehers 10 ай бұрын
sure, you can
@sg8nj
@sg8nj 2 жыл бұрын
I don't know what is the use of dynamically typed
@re.liable
@re.liable 2 жыл бұрын
With type hints, you help intellisense help you better :D
@codingnovice
@codingnovice 2 жыл бұрын
Nice I would have just used ChatGPT! I've been learning that way. I share the stuff I build too.
@Indently
@Indently 2 жыл бұрын
Good on you mate.
@mattmess1221
@mattmess1221 9 ай бұрын
Type hints actually can speed up your code, but you have to compile it with mypyc.
@pokerchannel6991
@pokerchannel6991 3 ай бұрын
may not speed up your code, but it speeds up your coding because you make fewer mistakes so that the code breaks less often
@bicycleninja1685
@bicycleninja1685 Жыл бұрын
Type hints does speed things up if you Cythonize the class
@sorvex9
@sorvex9 2 жыл бұрын
What some people think type hinting does, JIT tracing actually will do.
@francescoferazza9341
@francescoferazza9341 2 жыл бұрын
Love your stuff!
@Indently
@Indently 2 жыл бұрын
Grazie bello :)
@ThankYouESM
@ThankYouESM 10 ай бұрын
Dang... I was hoping for it to optimize the performance.
@Indently
@Indently 10 ай бұрын
Apparently it can if you look into some called "mypyc"
@ThankYouESM
@ThankYouESM 10 ай бұрын
@Indently Thank you, I will definitely look into that right away.
@LuccaSouzaXG
@LuccaSouzaXG 2 ай бұрын
Nice.
@vitu_9403
@vitu_9403 2 жыл бұрын
ok, but what is that "->" ???
@chaddaifouche536
@chaddaifouche536 2 жыл бұрын
That's the return type (the type of the value returned from the function). If you don't specify it, it would mean that your function returns None (or anything else really, as Indently said, type hints in Python are completely optional and not checked by cpython at all). If you mean why does his -> look like a real arrow, that's probably down to his IDE configuration, or eventually the font he's using (some specialized fonts use ligatures to make common code combination like -> or
@rondamon4408
@rondamon4408 10 ай бұрын
I thought it was just to make source code bigger
@TheRythimMan
@TheRythimMan 3 ай бұрын
I feel like this is a bad example for explaining it. People who have been coding for years will know what you mean, but i bet beginners will struggle to understand how: banana: Fruit = Fruit() Or integer: int = 1 ...is more readable. In fact, you might argue it's overly verbose and less readable as a result. I think a better example might be when you're asigning the return value of a function to a variable: main_fruit: Fruit = get_input(params) where it's less obvious in this situation if the variable is to be a string or dict or object It's also great for defining functions. But part of the appeal of python is not having to assign types, I feel like if you need to type annotate EVERYTHING you're trying to make python into something it's not - a statically typed language.
@denizsincar29
@denizsincar29 2 жыл бұрын
type hints must absolutely throw an exception if you mismatch types. let's go towards static typing, dynamic is an old tradition guys.
@Indently
@Indently 2 жыл бұрын
I like to think of Python as a free language. It lets you do what you want, even if what you want isn't always what you want ahah. They shouldn't require it in vanilla Python, but possibly provide us an option for enabling that, such as a special import, or with a special version of Python that replicates TypeScript.
@denizsincar29
@denizsincar29 2 жыл бұрын
@@Indently type #@type=static... something like that.
@Indently
@Indently 2 жыл бұрын
That would be neat!
@logana.martinez770
@logana.martinez770 2 ай бұрын
Dreamberd type annotations
@IgorGuerrero
@IgorGuerrero 2 жыл бұрын
Please at least use them as arguments and return values... I do not use it in cases like variable declaration when the value assigned is static and easily resolved by the Python's LSP server.
@GamingAbroad90
@GamingAbroad90 2 жыл бұрын
Idently: Python is meant for simplicity, if you want something else just use a lower level language also Idently: uses typing that low level need to work anyway me: *confused thinking*
@Indently
@Indently 2 жыл бұрын
I can't believe you called me Idently, twice, Simon 😅
@GamingAbroad90
@GamingAbroad90 2 жыл бұрын
@@Indently hahahahaha didn't even notice
@chaddaifouche536
@chaddaifouche536 2 жыл бұрын
Typing does not determine the level of your language though. You have very high level languages that are typed (try Haskell) and you can do most low level stuff in any language (try embedded programming in Python, it sure is higher level than in C but you'll often find yourself sending binary codes to explicit pins on the board and so on) though that is not recommended for speed and memory control. Dynamic typing (types are attached to values, variables and functions don't have specific types, all typing is done at runtime) vs Static typing (variables and functions have specific types, which are decided at "compile" time) is more a matter of practical implementation and philosophy around programming. Note that some developers think they dislike static typing but they in fact lack experience with modern languages and confuse static typing with explicit typing (every variable/function type has to be explicitly written when they're declared) whereas almost all modern typed language can infer most types without specifying them in your code (even if the recommendation is generally to put type annotations at least on your top-level declarations, to get better error messages). Type inference is such a win that most traditional languages have been retrofitted with it in the last decade.
@KA3AHOBA94
@KA3AHOBA94 2 жыл бұрын
__int__ lol haha
5 Good Python Habits
17:35
Indently
Рет қаралды 692 М.
The Best Band 😅 #toshleh #viralshort
00:11
Toshleh
Рет қаралды 22 МЛН
How to treat Acne💉
00:31
ISSEI / いっせい
Рет қаралды 108 МЛН
She made herself an ear of corn from his marmalade candies🌽🌽🌽
00:38
Valja & Maxim Family
Рет қаралды 18 МЛН
10 Important Python Concepts In 20 Minutes
18:49
Indently
Рет қаралды 443 М.
5 Reasons Why You Should Use Type Hints In Python
13:54
ArjanCodes
Рет қаралды 109 М.
Avoid These BAD Practices in Python OOP
24:42
ArjanCodes
Рет қаралды 83 М.
Compiled Python is FAST
12:57
Doug Mercer
Рет қаралды 123 М.
Why Rust is NOT a Passing Fad...
8:54
Travis Media
Рет қаралды 50 М.
Why You Should Think Twice Before Using Returns in Python
21:27
ArjanCodes
Рет қаралды 52 М.
5 Tips To Write Better Python Functions
15:59
Indently
Рет қаралды 116 М.
5 Useful Dunder Methods In Python
16:10
Indently
Рет қаралды 66 М.
PLEASE Use These 5 Python Decorators
20:12
Tech With Tim
Рет қаралды 128 М.
The Best Band 😅 #toshleh #viralshort
00:11
Toshleh
Рет қаралды 22 МЛН