5 Reasons Why You Should Use Type Hints In Python

Even though you don't see type hints all that often in Python code, and they'll probably never become obligatory, I still think you should use them. Here are 5 reasons why.
💡 Get my FREE 7-step guide to help you consistently design great software: arjancodes.com/designguide.
💻 ArjanCodes Blog: www.arjancodes.com/blog
🛒 GEAR & RECOMMENDED BOOKS: kit.co/arjancodes
👍 If you enjoyed this content, give this video a like. If you want to watch more of my upcoming videos, consider subscribing to my channel!
💬 Discord: discord.arjan.codes
🐦Twitter: / arjancodes
🌍LinkedIn: / arjancodes
🕵Facebook: / arjancodes
👀 Code reviewers:
- Yoriz
- Ryan Laursen
- James Dooley
- Dale Hagglund
🎥 Video edited by Mark Bacskai: / bacskaimark
🔖 Chapters:
0:00 Intro
1:27 What are type hints?
3:35 Criticisms of type hints
4:36 #1 Type hints help write shorter documentation
5:50 #2 Type hints improve the editing experience
8:25 #3 Type hints make coupling more explicit
9:08 #4 Type hints force being explicit about data structures
9:50 Sidestep: Type-driven development
11:31 #5 Type hints simplify your code
12:02 Type hints vs unit tests
12:43 Final thoughts
#arjancodes #softwaredesign #python
DISCLAIMER - The links in this description might be affiliate links. If you purchase a product or service through one of those links, I may receive a small commission. There is no additional charge to you. Thanks for supporting my channel so I can continue to provide you with free content each week!

Пікірлер: 252

  • @ArjanCodes
    @ArjanCodes2 жыл бұрын

    LEGO people *were* hurt in this video.

  • @aflous

    @aflous

    2 жыл бұрын

    Reported to KZread 👮 🚨

  • @Simon-yf7fo

    @Simon-yf7fo

    2 жыл бұрын

    If you don’t use type hints you fucking deserve it lmao

  • @AndreaDalseno

    @AndreaDalseno

    2 жыл бұрын

    I liked the joke. Actually, KZread subtitles translated your sentence as "be an Arian" and it sounds a little bit scary, doesn't it? 😵‍💫​

  • @cdhit

    @cdhit

    2 жыл бұрын

    Hi Arian, love your videos definitely. Could you make an episode about how to use Python package and to structure Python code to make project nice and clear ? @Arian

  • @RBBlackstone

    @RBBlackstone

    2 жыл бұрын

    My condolences for Bernard ( AKA Wilhelm).

  • @HexenzirkelZuluhed
    @HexenzirkelZuluhed2 жыл бұрын

    "I don't want to push you in any specific direction" he said, while dismembering Person B.

  • @terencetsang9518

    @terencetsang9518

    2 жыл бұрын

    Dumbledore asked calmly

  • @wesselbindt
    @wesselbindt2 жыл бұрын

    My main gripe with type hints is that they have a tendency to give a false sense of security. I've lost count of the times where I and my colleagues thought some piece of code was type safe only to later find out that something was inferred to be of type Any because of a poorly typed 3rd party library or some forgotten subscripts on a generic (the idea usually being that surely mypy should be able to infer this). Sure, many of these instances could've been avoided by configuring mypy to be as strict as possible, but in projects which started out untyped and are in the process of migrating to using types, this is simply not realistic. Still, in spite of this, it is absolutely beyond me that someone would choose to write a large production system in completely untyped code. The horror

  • @andyblue953

    @andyblue953

    2 жыл бұрын

    I'm always wondering why developers would switch at all to Python for larger productions? It's not like there aren't good alternatives.

  • @tkerkvliet

    @tkerkvliet

    2 жыл бұрын

    isn't that just the perfect description of development reality: fixing shit of people that made choices that are absolutely beyond us

  • @alex952

    @alex952

    2 жыл бұрын

    @@andyblue953 because of its extremely fast turn around for development and easy of use/learn. It’s also got many libraries that help you deliver code. As a company you can enforce documentation, testing, etc. which helps overcome the drawbacks of the language.

  • @newperspective5918

    @newperspective5918

    2 жыл бұрын

    ​@@alex952 Tbh I don't really see easy of use/learn to be a mayor point. For me it is all about libraries and the ability of the language to solve the problem. Python is a bad choice if type safety or high performance is key, so just pick something else, e.g. C++. I think Python, C++, HTML, CSS and JS is all the languages you need to know in most cases. Then add som SQL, bash and UNIX knowledge on that and you can basically solve any problem.

  • @stephens3909

    @stephens3909

    2 жыл бұрын

    Where third party libraries are involved, this is where type casting comes in, especially if you are taking advantage of abstract interfaces and some OO patterns when using some third party libraries.

  • @derGerlach
    @derGerlach2 жыл бұрын

    When I started with Python, TypeHints were not available. Then I had to work with Springboot wich uses Java. I really loved the statical typed style. It just forced me to write better code. Nowadays I use type hinting in every piece of Python code and i love it. It helps so much, especially with bigger projects. Great video, keep it up 👍👍

  • @roelmathys

    @roelmathys

    2 жыл бұрын

    But it's a game of hints in Python. No statistical analysis is done, in Java it would not compile and would not run. And it's getting better, but it's making code much more verbose.

  • @redcrafterlppa303

    @redcrafterlppa303

    2 жыл бұрын

    @@roelmathys a good rule of thumb is when your types get out of hand and look verbose and cluttered, rethink your design decisions.

  • @roelmathys

    @roelmathys

    2 жыл бұрын

    @@redcrafterlppa303 Python existed for two decades without this. Python doesn't do anything to improve type safety based on type hints. You still can run the code if the type hints indicate it should not run (mypy). It's bolted on and feels more like documentation than something else.

  • @redcrafterlppa303

    @redcrafterlppa303

    2 жыл бұрын

    @@roelmathys only because it runs doesn't guarantee its good code. The level of control a strongly typed language provides python can never achieve. Overall in my opinion python is a really ugly language with to much dynamic and "it works somehow" in it. I don't get what to even use python for.

  • @roelmathys

    @roelmathys

    2 жыл бұрын

    @@redcrafterlppa303 statically compiled languages give type guarantees but do not guarantee correctness or good code. But I do not understand what you are commenting about. If you don't want Python, what are your remarks adding?

  • @cemsity
    @cemsity2 жыл бұрын

    My use case for type hints is mostly for the IDE Intellicode. If i have a larger application then using type hints with mypy keeps me sane and prevents a lot of dumb bugs. Also the fact that type hints aren't required enable the developer to omit type hints where they aren't need or obvious.

  • @simmonslucas

    @simmonslucas

    2 жыл бұрын

    I guess that this benefit should have been apparent to me, but I didn't realize. A great time saver to find errors before you debug.

  • @rban123

    @rban123

    2 жыл бұрын

    yeah especially on a big project I'll typically have a CI job which runs mypy automatically on every PR and fails the PR if there are any type errors

  • @TomTrval
    @TomTrval2 жыл бұрын

    Agreed👍. Type hints are especially helpful for me in 2 cases> -when I return to my code after long time or study code from someone else🤯 -early detect problems with code architecture. I learned when I am thinking to my self "Hmm typing this code is hard" it means there is something wrong with architecture (usually I missed abstraction to decouple modules)

  • @roelmathys

    @roelmathys

    2 жыл бұрын

    Type hints are just hints, if somebody puts them in, and the next person does not respect the "contract", the application still runs.

  • @Aldraz
    @Aldraz Жыл бұрын

    My personal strongest arguments for using or not using type hints are these: Pros - Using type hints will actually make your code much faster one day, because libraries like mypy, etc will use it to compile to much faster code. Cons - It clutters the screen and the more type hints you have the less immidiate information you have in some sense, so maybe using it for every function isn't the best idea.

  • @Ludwighaffen1

    @Ludwighaffen1

    7 ай бұрын

    Another "con" is that, at least in data science, many bugs come from the wrong dimensionality of a data structure, or the wrong formatting of a specific table column, or a column not existing. These bugs can only be properly handled by managing exceptions inside functions and type hints is not really helping.

  • @mamazu1995
    @mamazu19952 жыл бұрын

    It's a great video and I completely agree that types are a great way to ensure correctness and proper scope of functions. One thing that I would also like to add to point 4 is that, if your program is sufficiently typed then you can do "type assisted refactoring". For example you want to replace a property of an object with a method call or something. Just remove the property from the object and then run a static analyzer like mypy and see where the code fails. Then you know which places to fix. This is much easier than trying out all possible paths that could crash the program.

  • @CornishMiner
    @CornishMiner2 жыл бұрын

    I've picked up the habit, and I like it. It helps me a lot with keeping track of what I'm doing, it reduces documentation and it helps make the most of the ide.

  • @wylde780
    @wylde7802 жыл бұрын

    This video is prefectly timed as i recently came across some type hints in some airflow code and didn't understand it. Thanks

  • @ErnestGWilsonII
    @ErnestGWilsonII6 ай бұрын

    I just want to say thank you very much for making these videos and sharing them with all of us. I only recently found your channel and I am now subscribed with notifications enabled and thumbs up! Thank you, Ernie in Pennsylvania

  • @ArjanCodes

    @ArjanCodes

    5 ай бұрын

    Thank you for the kind comment, Ernie!

  • @robertbrummayer4908
    @robertbrummayer49082 жыл бұрын

    Great video Arjan. I also use type hints a lot in my code. I recommend mypy as type checher. It can easily be integrated into VS code.

  • @arnold9103
    @arnold91032 жыл бұрын

    We started using those at work! Improved readability and quality somehow !

  • @user-hf5be1zu7s
    @user-hf5be1zu7s2 жыл бұрын

    A team for the win!! Once again, great video. notes are taken!

  • @ArjanCodes

    @ArjanCodes

    2 жыл бұрын

    Thank you, glad you liked it!

  • @SophieJMore
    @SophieJMore2 жыл бұрын

    3:06 minor correction, before 3.9 I remember I had to change all my type hints in one of my projects to make it compatible with python 3.8 so that it could run on windows 7

  • @lunarmagpie4305

    @lunarmagpie4305

    2 жыл бұрын

    3.9+ style type hints work in 3.8 if you do `from __future__ import annotations` at the top of the file

  • @SophieJMore

    @SophieJMore

    2 жыл бұрын

    @@lunarmagpie4305 Interesting. Thank you, I didn't know that

  • @anyyyel
    @anyyyel Жыл бұрын

    Dude, your channel is really good. Congratulations! Simple, objective.

  • @ArjanCodes

    @ArjanCodes

    Жыл бұрын

    Glad you like the videos!

  • @xor2003
    @xor2003 Жыл бұрын

    * Type hints in Python are optional * They can aid with debugging and avoiding outdated documentation * They also assist with autocomplete options in IDEs and make code easier to read and write * Python 3.10 allows for lowercase type hints for certain data types * Using type hints improves code quality and catches errors early on * It promotes explicit data structure design and simplifies code-writing * Type-driven development clarifies how data will interact and detects issues with complex functions * Pydantic and type hints improve data understanding, focus unit tests, and offer faster and accurate accuracy checking.

  • @wyst69
    @wyst69 Жыл бұрын

    Your recorded the whole video in one single shot ! That's clean.

  • @aashayamballi
    @aashayamballi2 жыл бұрын

    Just wanna say.. thank you, Arjan! Have learned a lot from you on Python 🙏

  • @ArjanCodes

    @ArjanCodes

    2 жыл бұрын

    You’re welcome - glad to hear that the content is helpful to you!

  • @lowkeygaming4716
    @lowkeygaming4716 Жыл бұрын

    Nice vid. Very insightful.

  • @ArjanCodes

    @ArjanCodes

    Жыл бұрын

    Thank you, glad you liked the video!

  • @tugrulpinar16
    @tugrulpinar162 жыл бұрын

    Thanks a lot for expanding on this

  • @ArjanCodes

    @ArjanCodes

    2 жыл бұрын

    You’re welcome!

  • @kosmonautofficial296
    @kosmonautofficial2962 жыл бұрын

    Interesting video thanks

  • @NStripleseven
    @NStripleseven2 жыл бұрын

    Another nice ide-related thing that type hints improve is autocomplete. If your ide knows that a variable is supposed to be an int, it can give you suggestions that make sense for an int value.

  • @tronicit

    @tronicit

    8 ай бұрын

    I think that's probably the best reason to use them. That and the methods which work as soon as you apply the hints. It's often easier to pick a method or related function from the list than trying to remember the exact syntax which doesn't work if you haven't got it correct.

  • @bluesdog88
    @bluesdog882 жыл бұрын

    Arjan you are a strange man, that's mostly why I subscribed....keep up the good work and code!

  • @dougalscott4896
    @dougalscott48962 жыл бұрын

    I teach people coding (i.e. programming, not just python) and I find type hints very useful. It gets the student to think about the code that they are writing before they start bashing away at the keyboard, and if they need to change the types it probably means that they need to change the code.

  • @Pirake123
    @Pirake1232 жыл бұрын

    Type hints actually make python more enjoyable. Putting them in the language means we don't get a Tython language down the track (like Typescript for JavaScript)

  • @roelmathys

    @roelmathys

    2 жыл бұрын

    I don't know, it's a lot of extra typing, a lot of extra verbosity. And it's not that it's statically typed at that moment, it will just run fine, wel :) Statically typed languages result in not being able to execute the application. Type hinting is just that: hints. It - currently - does not make code magically safer, it depends on running mypy ... , or makes the generated application faster..

  • @ea3gug
    @ea3gug2 жыл бұрын

    Hey!, excellent video as always, very educational. Thanks!. I am oriented into app security, and I have found a paralelism about this technique and a tool called 42Crunch that does unit testing against an API definition (Swagger, OpenAPI), doing unit tests against the type definition to check the specification against the real input/outuput, messing with parameters, so it is the same concept, to check that development meets specification.

  • @ArjanCodes

    @ArjanCodes

    2 жыл бұрын

    Thanks for sharing!

  • @nisheetpatel2654
    @nisheetpatel26542 жыл бұрын

    I often forget to hit the like button until the time you mention it, but this time, with the lego people in the first 20 seconds, I knew this video was legendary!

  • @victormungai
    @victormungai Жыл бұрын

    In one of the latest updates(at the time of writing this comment) to the python extension on vscode, one can instruct vscode to activate the type checker. I have mine set to strict and I'm glad I did.

  • @fexofenadinaGenerica
    @fexofenadinaGenerica2 жыл бұрын

    I agree. Type hints help to better understand the code.

  • @liquidpebbles
    @liquidpebbles2 жыл бұрын

    I recently moved from a C# job to Python... Man, I can't stand dynamically typed languages. The development experience with a typed language is far superior in my opinion as it opens up so many possibilities for the IDE.

  • @Michal_Peterka

    @Michal_Peterka

    2 жыл бұрын

    I prefer the way F# works. You don't have to specify the type at most of time but it is still strongly typed. And there is VS Code extension Ionide that displays the types annotation for you.

  • 2 жыл бұрын

    @@Michal_Peterka type inference of ML-like languages are awesome, F# is underrated

  • @joningram
    @joningram2 жыл бұрын

    You briefly mention 'duck typing' in the beginning, and I think this is the issue I have with the mass importation of type hints into Python - they miss that what I want to specify in a function header is not the type I require of an input variable, but instead what capabilities that variable should have. For example, if I am writing a 'max' function, then I don't want to restrict it to a list, or a tuple, or any inbuilt type - I want to accept anything which can be iterated through. There are various guises of this idea - interfaces, templates, contracts - in other languages and I am hoping that this is the direction that typing will go in Python. The other issue I have with typing is that it can make quite simple ideas (like passing a function into another function) complicated purely through awkward syntax. I have been able to explain and demonstrate ideas such as callback functions very easily in Python which I would have struggled to write in a language which required types.

  • @DajesOfficial

    @DajesOfficial

    2 жыл бұрын

    You have types such as Iterable and Callable for that specific purposes

  • @willemvdk4886
    @willemvdk48862 жыл бұрын

    The Wilhelmscream had me, haha. And the fact that you used Uncle Bob's book lol

  • @ArjanCodes

    @ArjanCodes

    2 жыл бұрын

    Thanks :)

  • @Ludwighaffen1
    @Ludwighaffen17 ай бұрын

    Thanks for this great video. In your neat example on the luhn_checksum function (I liked your lunatic joke btw 🙂), I'd argue that "number" is a pretty bad variable or argument name in the first place. But we all Bernards got your point. ;-)

  • @colletantoine8885
    @colletantoine88852 жыл бұрын

    Thanks for this great video ! How to you manage type hints with bumpy in Vscode ? I always end up with a lot of warnings although I use bumpy.typing ...NDArray etc.

  • @TechieGuy82
    @TechieGuy822 жыл бұрын

    Love you videos.. Actually "your bad joke" made me laugh :D Thumbs up

  • @ArjanCodes

    @ArjanCodes

    2 жыл бұрын

    Thanks! 😃

  • @Shivnaren
    @Shivnaren2 жыл бұрын

    Thanks a lot, Arjan-learned all about typing and types from you. One downside is I can't stand to read untyped python anymore...unfortunately there's a lot of it about :D

  • @andyblue953
    @andyblue9532 жыл бұрын

    I use Python for data science purposes and am perfectly happy with dynamic types for rapid prototyping. I use type hints only very rarely and prefer default values instead. But it seems that more and more serious programmers move to Python as well, to write larger projects with a stronger need for type control. Since there's plenty of excellent alternatives with static types, I always wonder about their motivation to switch... ?

  • @ArjanCodes

    @ArjanCodes

    2 жыл бұрын

    What I often see in the companies I’m working with is that they start with a simple data science project in Python, but then things become more complex. They want to keep using all the useful libraries in the Python ecosystem, but they want to add an API layer to the data processing system. Or what about doing complex computations in parallel? Or how about running all those things in the cloud? Or what if you want to make the code generic so you can use it for multiple clients? Or add authentication to control access to the data? Etc etc. It’s at this stage where you can really benefit from a more strict approach to writing Python code to keep things manageable.

  • @Witpaardt

    @Witpaardt

    2 жыл бұрын

    ​@@ArjanCodes that is more a cultural problem and by definition not generalizable. I think you make good points in your video, but @Andreas Groth does as well. Also microservices. In many companies those data science scripts might turn into a small service where typing can be useful, but not necessary. There your final point about short scripts remains valid. Furthermore, naming things clearly can also help with documentation (as you can see in your video). The thing about Python is, the language is flexible and that is a good feature imho.

  • @thomassmail6896
    @thomassmail68962 жыл бұрын

    This is really cool, I wasn’t even aware of them. I feel they are something I might add at the end of writing a function when I go back and comment my code.

  • @ArjanCodes

    @ArjanCodes

    2 жыл бұрын

    Thanks so much Thomas, glad it was helpful!

  • @NicholasShanks

    @NicholasShanks

    Жыл бұрын

    Try adding them right at the start when you don't even know how the function is going to work yet.

  • @sandie_jr
    @sandie_jr5 ай бұрын

    Finally, I'm convinced!

  • @ArjanCodes

    @ArjanCodes

    5 ай бұрын

    Glad the video was useful then! Ahah

  • @andrewglick6279
    @andrewglick62792 жыл бұрын

    I love type hints in Python and I always try to use them. For people wanting safe validation from type hints, checkout the Beartype library (it is a decorator you can add to functions that verifies your arguments with your type hints). I do think type hints have ways to go, and part of that may be Python's refusal to enforce them even as part of convention. For example, there is something so much more elegant about `foo?: int` in TypeScript than `foo: Optional[int]` (I hate having to import Optional). The lack of type hinting enforcement in libraries also makes it difficult for IDEs to work at their full potential. And don't even get me started on how hideous the `Callable` type hint is. Though generally speaking, I think Python is headed on the right track for improving type hints.

  • @blutella4162
    @blutella41622 жыл бұрын

    Great video.

  • @ArjanCodes

    @ArjanCodes

    2 жыл бұрын

    Thank you!

  • @sillytechy
    @sillytechy2 жыл бұрын

    Hi could you recommend a book on software design. Like how to design a large scale software with multiple elements like database, cloud elements(AWS/AZURE), break into smaller components, make Class relationship diagrams to use OOPs properly. Any book that talks on this.

  • @JeffreyChadwell
    @JeffreyChadwell2 жыл бұрын

    Nice use of the Wilhelm Scream!

  • @ArjanCodes

    @ArjanCodes

    2 жыл бұрын

    Ha, thanks! 😁

  • @ivhea
    @ivhea2 жыл бұрын

    Density of jokes per minute is augmenting with each new video. At some point this will become a comedy channel. Just a fact, not a judgement. :)

  • @ArjanCodes

    @ArjanCodes

    2 жыл бұрын

    You got me!

  • @zackadam2598
    @zackadam2598 Жыл бұрын

    I liked this video explicitly for the LUNA dig. Spicy! 🌶

  • @bluzter
    @bluzter2 жыл бұрын

    Type hints are kinda satisfying which is why I love them.

  • @johnjsal
    @johnjsal9 ай бұрын

    Is that VS Code you were using? What is the name of that syntax theme?

  • @Celestialzzz
    @Celestialzzz2 жыл бұрын

    in vs.code for me type hinting does not flag a type mismatch error as you display at 7:55 in your video. Is there an extension that you have to download first to have this work?

  • @riptorforever2
    @riptorforever22 жыл бұрын

    Psico introduction with Dexter's serial killer flavor.. loved!

  • @ArjanCodes

    @ArjanCodes

    2 жыл бұрын

    Tonight’s the night…

  • @quaxBK
    @quaxBK2 жыл бұрын

    The strongest argument for type hints are for the LSP imo, you get much better diagnostics and completions that way.

  • @nargileh1

    @nargileh1

    2 жыл бұрын

    yeah code completion is the best thing I get out of type hints; with CTRL-click to jump to definition code in second place

  • @sorvex9

    @sorvex9

    7 ай бұрын

    Literally the primary reason I use them lol

  • @kayakMike1000
    @kayakMike10002 жыл бұрын

    Type hints are sorta like stronger typing which is pretty good... Wasn't available when I started with python, but I can see great value.

  • @roelmathys

    @roelmathys

    2 жыл бұрын

    Python was already strongly typed - has always been told. You do not get a statically type checked language with these type hints. It's more for documentation purposes.

  • @distant6606
    @distant66062 жыл бұрын

    I also recommend using mypy to check for these type hints.if u type hint a variable to str and then assign to it an integer mypy will throw you an error.

  • @TheSzybas
    @TheSzybas Жыл бұрын

    That "luhnatic" joke was perfect!

  • @ArjanCodes

    @ArjanCodes

    Жыл бұрын

    Thank you Tomasz, glad you liked the video!

  • @p.t.8312
    @p.t.83122 жыл бұрын

    Do I need a special VS Code extension to highlight incorrect types in, for example, function arguments? Looks like it doesn't work by default.

  • @rachinini
    @rachinini2 жыл бұрын

    Python as a simple scripting language that is used for writing small to medium-sized programs is beautiful. Adding type hints, redundant features (how many string interpolation methods do you need?), and other excess features has turned Python into an ugly, unstable mess.

  • @sync9827
    @sync98272 жыл бұрын

    The PyCharm IDE checks if the types of the arguments and of the return value match with the types specified in the docsring. So while you're writing the code the IDE checks if all the types are correct even though you aren't using any type hints. So you can do type-driven development without type hints. But since type hints are pretty elegant and less work than writing docstrings, I might also start using type hints...

  • @hansdietrich1496

    @hansdietrich1496

    8 ай бұрын

    Also, PyCharm hints become much better, when you're generous with adding type hints to your code.

  • @airatvaliullin8420
    @airatvaliullin8420 Жыл бұрын

    I don't want to push you in any particular direction *beheads a lego man who is against typehinting*

  • @JuampyRabino
    @JuampyRabino2 жыл бұрын

    Love that you used Wilhelm's scream at the beginning 🤣🤣🤣🤣

  • @ArjanCodes

    @ArjanCodes

    2 жыл бұрын

    It's my favorite sound effect :)

  • @kurkabane702
    @kurkabane7022 жыл бұрын

    Can someone explain why the function argument 'number' is reffered further as 'nr'? Is it some recent feature?

  • @postmodernist1848
    @postmodernist1848 Жыл бұрын

    After learning a lot of Python I tried C and it really changed my perspective. At first, I thought that static typed languages are inferior (are dumb??) and then I realised that it's much, much easier to understand and maintain typed code. Static typing is a great feature, not a disadvantage. When I came back to Python, I knew I needed to use (optional) typing

  • @ThatOpinionIsWrong

    @ThatOpinionIsWrong

    Жыл бұрын

    You mean Optional[typing] ? lol

  • @Ziggity
    @Ziggity2 жыл бұрын

    I started a job at a young startup last year, backend was written in python and was completely untyped. I was losing my mind. These days after pushing hard in that direction everything is typed and I am a happy dev

  • @user-xc5cx7lh4l
    @user-xc5cx7lh4l9 ай бұрын

    For complex tasks where we need types there are Java or C#... Python and JS does not need types, they are for rapid development. Type hints are ok until you need to use multiple generics with restrictions. And then you need more time to write type definitions than the actual code.

  • @handle_and_gretel
    @handle_and_gretel Жыл бұрын

    use type hints and then vscode extension AutoDocString to create the small snippet for documentation

  • @cdhit
    @cdhit2 жыл бұрын

    Hi Arian, love your videos definitely. Could you make an episode about how to use Python package and to structure Python code to make project nice and clear ? @Arian

  • @miguelvasquez9849
    @miguelvasquez98492 жыл бұрын

    How should i type when a function receive an object that is not imported in my module? Should i import it only for the hinting?

  • @josewilhelm2717
    @josewilhelm27172 жыл бұрын

    Great video! Can we have a discussion video about Object Oriented Programming and why it is being criticized so much lately?

  • @HelloWorlds__JTS

    @HelloWorlds__JTS

    2 жыл бұрын

    @ArjanCodes already has a handful of videos discussing tradeoffs between functional and object oriented programming. Just search for object oriented in the search bar on his main page, and the first 5-6 videos are probably exactly what you're looking for.

  • @josewilhelm2717

    @josewilhelm2717

    2 жыл бұрын

    @@HelloWorlds__JTS oh thanks, I didn't know. I will search it out!

  • @LewisCowles
    @LewisCowles2 жыл бұрын

    I Liked this. Another reason to use them (I wrote you an email about not liking type-hints) is that it can make it easier for compliant tools to assist a coder in refactoring in certain code-bases. I Still don't type-hint everything, but have found myself using type-hints more and more. Do you think Guido comments have proven to be unhelpful in use of this language feature, or perhaps are misinterpreted?

  • @ArjanCodes

    @ArjanCodes

    2 жыл бұрын

    Thanks Lewis! Your email was one of the reasons I created the video 😊. I can’t speak for Guido, but programming languages are always changing, leading to new insights into how to make things better which in turn changes our opinions about things. I’m in favor of a stricter Python, while at the same time not losing the flexibility and simplicity of the language in its current form. But let’s see, that might also change in the future 😁.

  • @davethorn9423
    @davethorn9423 Жыл бұрын

    If I had a function expecting a string , surely don't call the argument number , call it num_str or something ? Or am I missing something . Just clear naming seems to be a solution ?

  • @mesaber86
    @mesaber86 Жыл бұрын

    Awesome intro :D

  • @ArjanCodes

    @ArjanCodes

    Жыл бұрын

    Thanks MeSaber, happy you’re enjoying the content!

  • @FabioKasper
    @FabioKasper2 жыл бұрын

    Ohh, "type hint" is how it is called then. All I know is that I started using them after seeing it in your videos. Thanks again. 😂

  • @Pawl0solidus
    @Pawl0solidus2 жыл бұрын

    After working for years with Typescript I’m learning Python for some months and I can see extreme value in these type hints, they are really useful for the reasons mentioned in this video.

  • @presstv
    @presstv2 жыл бұрын

    Did you kill Person A along with Bernard The Headless? that's not fair🤣😂. great video Arjan :)

  • @maksimon519
    @maksimon519 Жыл бұрын

    in short, type hints make development much faster. it also help editor to auto-complete the name of attributes, which makes writing codes much faster and satisfying.

  • @matrixtoogood5601
    @matrixtoogood56012 жыл бұрын

    How to use type hints in frameworks like Django?

  • @cn-ml
    @cn-ml Жыл бұрын

    I just cannot grasp why people dislike using type annotations. The amount of coding errors type hints prevent is just insane. Even with statically typed languages people usually code bugs, but without type checking even it's almost impossible to write code that works the first try. I absoltely hate python without types.

  • @parisqs
    @parisqs Жыл бұрын

    Beware: If you don't write type hint, you LEGO body will be torn apart!

  • @HaiderGill-th5bp
    @HaiderGill-th5bp Жыл бұрын

    I was always told to prefix my variable names with int_, flo_, str_, boo_ in C so I know what type it is, I expanded it for C++ obj for object why do it because datetime could be an datetime obj or a string holding a datetime...

  • @samsung40_media87
    @samsung40_media872 жыл бұрын

    I do it automatically, it doesn't feel like extra work and follows on from explicit > implicit and defining the return type as a way to guide others so as not to use mixed return types

  • @Han-ve8uh
    @Han-ve8uh2 жыл бұрын

    7:44 we see the red underline error because the value is hardcoded, and is a simple primitive type right? In real programs we likely read information from a file or env, does that mean the typechecker will not be able to know whether the contents are right, since we haven't even provided the file or setup the env yet during development. I'm trying to get a practical sensing of whether this benefit is really that big a benefit. 10:51 is the concept in the popup here that "input type hints can point out mistakes in output type hints"? Which means if we never wrote the input hints but only the output hint, there won't be warnings?

  • @totalermist

    @totalermist

    2 жыл бұрын

    > we see the red underline error because the value is hardcoded, and is a simple primitive type right? No. The IDE will show the same error if you pass a variable of the wrong type (the IDE can infer the type of the variable even without type hints, since it has to be assigned to at some point). This of course only works reliably if the functions or libraries you use also have type hints.

  • @snippletrap
    @snippletrap Жыл бұрын

    Arjan you have the best programming content and the high production values are impressive. But if you're going to show your arms, you ought to tone them up!

  • @TheKubaski
    @TheKubaski Жыл бұрын

    I noticed that you use a tab to debug the python code. Does anyone know how to set that up?

  • @Betacak3
    @Betacak32 жыл бұрын

    Source code is meant to be read by humans. You only ever write a piece of code once, but you will read it many times. So, whenever you ask yourself "Is it worth it to spend this extra time to write this?", think of the time it could save when you have to deal with that bit of code 6 months or more later. That's my response to the "It's more work" argument and - in my experience - a good way to approach code in general. Don't write code for your computer. Write it for yourself and your collleagues.

  • @banatibor83

    @banatibor83

    2 жыл бұрын

    Very very good point. The most important attribute of code that it should be easily readable. That's why I like to use small functions with self explanatory names, and spend time on naming my variables. When things are well named it is easy to read and understand the code. Type hints start to get extremely helpful when multiple scrum teams are working on the same software. Unavoidable that these teams will develop a common library for certain tasks for that software, type hints in that common library can save many hours.

  • @bitswapper
    @bitswapper11 ай бұрын

    VSCode doesn't show the type mismatch in python when I use type hints

  • @Maroonight
    @Maroonight2 жыл бұрын

    I have a struggle to type hint my company's code. We have a python wrapper on C++ that defines a class inside a function. In that way, the type of the variable would be . I certainly cannot change how they write the wrapper, so how can I circumvent this problem?

  • @pasiasachilleas4739

    @pasiasachilleas4739

    2 жыл бұрын

    the problem is that is long? If yes you could probably define a short name for this.

  • @sanketower
    @sanketower2 жыл бұрын

    Doing OOP without type annotations is practically against the basic design patterns that define that paradigm. Typing in Python may not be the default behavior and is technically just sugar coating, but adding it to your already structured project can help you discover a lot of vulnerabilities and bad practices hidden behind your Python trickery. Like they always say; make it work, then make it right.

  • @DanielStoehr203
    @DanielStoehr203 Жыл бұрын

    Do you have a favorite Type checker? Pyright? Mypy? And why? Are there differences?

  • @wilkyarny3012
    @wilkyarny30122 жыл бұрын

    In case the next step of python update is to become strongly type language... Guido is certainly working on it in Microsoft. Like Microsoft provided typescript for Javascript devs. We can reasonably corroborate the same kind of path for python and a python runtime environment (like nodejs.) it's my opinion but nothing is sure for now

  • @ArjanCodes

    @ArjanCodes

    2 жыл бұрын

    If that happens, I would be very, very interested in that development.

  • @dennisvlahos

    @dennisvlahos

    2 жыл бұрын

    Typescript is statically typed though, which is something Python never will be

  • @tkerkvliet

    @tkerkvliet

    2 жыл бұрын

    Python is already considered a strongly typed language: we need explicit conversions between types and operations inappropriate to a type simply can not be performed. Strongly typed is something different than statically typed.

  • @nargileh1

    @nargileh1

    2 жыл бұрын

    What would be the point of strongly typed Python?? You can make your argument checking as stringent as you want without forcing the whole language to do it.

  • @hshhsjhahsvs7728
    @hshhsjhahsvs77282 жыл бұрын

    Am rooting for type hints included.

  • @cezhou9140
    @cezhou91406 ай бұрын

    Type hints is like you spent an eternity to get accustomed to duck typing and then it gets shattered to dust.

  • @cholobok

    @cholobok

    Ай бұрын

    Just use typing.Protocol

  • @hudabdulwahab2499
    @hudabdulwahab24992 жыл бұрын

    Is it possible to make type hint videos for cases like doubly-linked lists etc? e.g. it's a bit annoying when the node is a class object and we can't define their types as Class | None

  • @ianakotey

    @ianakotey

    2 жыл бұрын

    Use Optional from the typing module

  • @Andrumen01
    @Andrumen012 жыл бұрын

    Think about typehints as seat belts for code. In principle, they aren't needed, but during a crash they can save you!

  • @JorgeEscobarMX
    @JorgeEscobarMX2 жыл бұрын

    I have to agree that removing type checks on unit tests is removing a lot of boring repeating work. As long as we are all consenting adults, I'm removing type checks from my own unit tests.

  • @fartzy
    @fartzy2 жыл бұрын

    outstanding dad joke that was great

  • @davidedigrande3204
    @davidedigrande32042 жыл бұрын

    My internal OCD programmer COMMENDS that I add type hints in all of the code. Sometimes I do it in variables declarations too, when the type of the object is not obvious.

  • @CynicalOldDwarf
    @CynicalOldDwarf Жыл бұрын

    I like the concept, hate the syntax though. I wish they'd have kept a c-style typing syntax (as in type first, e.g. int a_number) instead of using typescript's syntax

  • @k98killer
    @k98killer2 жыл бұрын

    Might be preaching to the choir with this one, but I imagine some people haven't experienced the difference between coding with type hints and coding without them.

  • @ranjansingh9972
    @ranjansingh9972 Жыл бұрын

    Type Hints add to why I find Python to be a frustrating language. It’s a language that seems to support every paradigm of programming, and because of that it’s hard to know ‘the right way’. It’s just endless learning of new language features and tweaks - and there’s no escaping it because the libraries/frameworks you use pull in language features from across the spectrum. Probably been trying to learn/code in Python for 6 years and still feel like a complete novice. That same amount of time in an older language, like C, was enough for relative mastery (vastly different eras though, I concede).

  • @majolakrem77
    @majolakrem773 ай бұрын

    Say you have a class that has as an object another class, which in turn contains a reference to the parent class. Using type hints is quite useful as it allows you to navigate the structure of your class(es) easier while coding. However, it throws an error during execution as python detects circular class definitions. Currently, I code using type hints for such class dependencies but later erase the imports again. That's unfortunate...

  • @cholobok

    @cholobok

    Ай бұрын

    Use “from __future__ import annotations”

  • @cholobok

    @cholobok

    Ай бұрын

    It’s pretty cool to define recursive types that way too, like type Operator = Literal[“+”, -“] type Operand = int | float type Expression = Operand | tuple[Expression, Operator, Expression]

  • @davidzitzmann542
    @davidzitzmann5422 жыл бұрын

    If there is one feature in python that I could change than it is dynamic typing. At first it looked very convenient to use, but as soon as you work with code of others it starts to get very helpful when you know what is the output type another coder intended. Or even to remember to hint myself what I did a few "days" ago.^^