5 Reasons Why You Should Use Type Hints In Python

  Переглядів 102,427

ArjanCodes

ArjanCodes

День тому

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!

КОМЕНТАРІ: 247
@ArjanCodes
@ArjanCodes Рік тому
LEGO people *were* hurt in this video.
@aflous
@aflous Рік тому
Reported to UKposts 👮 🚨
@Simon-yf7fo
@Simon-yf7fo Рік тому
If you don’t use type hints you fucking deserve it lmao
@AndreaDalseno
@AndreaDalseno Рік тому
I liked the joke. Actually, UKposts subtitles translated your sentence as "be an Arian" and it sounds a little bit scary, doesn't it? 😵‍💫​
@cdhit
@cdhit Рік тому
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 Рік тому
My condolences for Bernard ( AKA Wilhelm).
@HexenzirkelZuluhed
@HexenzirkelZuluhed Рік тому
"I don't want to push you in any specific direction" he said, while dismembering Person B.
@terencetsang9518
@terencetsang9518 Рік тому
Dumbledore asked calmly
@derGerlach
@derGerlach Рік тому
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 Рік тому
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 Рік тому
@@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 Рік тому
@@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 Рік тому
@@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 Рік тому
@@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?
@wesselbindt
@wesselbindt Рік тому
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
@andreasgroth499
@andreasgroth499 Рік тому
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 Рік тому
isn't that just the perfect description of development reality: fixing shit of people that made choices that are absolutely beyond us
@alex952
@alex952 Рік тому
@@andreasgroth499 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 Рік тому
​@@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 Рік тому
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.
@cemsity
@cemsity Рік тому
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 Рік тому
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 Рік тому
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
@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 5 місяців тому
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.
@xor2003
@xor2003 11 місяців тому
* 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.
@TomTrval
@TomTrval Рік тому
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 Рік тому
Type hints are just hints, if somebody puts them in, and the next person does not respect the "contract", the application still runs.
@CornishMiner
@CornishMiner Рік тому
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.
@mamazu1995
@mamazu1995 Рік тому
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.
@SophieJMore
@SophieJMore Рік тому
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 Рік тому
3.9+ style type hints work in 3.8 if you do `from __future__ import annotations` at the top of the file
@SophieJMore
@SophieJMore Рік тому
@@lunarmagpie4305 Interesting. Thank you, I didn't know that
@wylde780
@wylde780 Рік тому
This video is prefectly timed as i recently came across some type hints in some airflow code and didn't understand it. Thanks
@arnold9103
@arnold9103 Рік тому
We started using those at work! Improved readability and quality somehow !
@Pirake123
@Pirake123 Рік тому
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 Рік тому
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..
@robertbrummayer4908
@robertbrummayer4908 Рік тому
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.
@liquidpebbles
@liquidpebbles Рік тому
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 Рік тому
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.
@danieldeviveirosinacio
@danieldeviveirosinacio Рік тому
@@Michal_Peterka type inference of ML-like languages are awesome, F# is underrated
@ErnestGWilsonII
@ErnestGWilsonII 3 місяці тому
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 3 місяці тому
Thank you for the kind comment, Ernie!
@wyst69
@wyst69 Рік тому
Your recorded the whole video in one single shot ! That's clean.
@dougalscott4896
@dougalscott4896 Рік тому
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.
@user-hf5be1zu7s
@user-hf5be1zu7s Рік тому
A team for the win!! Once again, great video. notes are taken!
@ArjanCodes
@ArjanCodes Рік тому
Thank you, glad you liked it!
@anyyyel
@anyyyel 10 місяців тому
Dude, your channel is really good. Congratulations! Simple, objective.
@ArjanCodes
@ArjanCodes 10 місяців тому
Glad you like the videos!
@joningram
@joningram Рік тому
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 Рік тому
You have types such as Iterable and Callable for that specific purposes
@NStripleseven
@NStripleseven Рік тому
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 5 місяців тому
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.
@ea3gug
@ea3gug Рік тому
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 Рік тому
Thanks for sharing!
@tugrulpinar16
@tugrulpinar16 Рік тому
Thanks a lot for expanding on this
@ArjanCodes
@ArjanCodes Рік тому
You’re welcome!
@aashayamballi
@aashayamballi Рік тому
Just wanna say.. thank you, Arjan! Have learned a lot from you on Python 🙏
@ArjanCodes
@ArjanCodes Рік тому
You’re welcome - glad to hear that the content is helpful to you!
@nisheetpatel2654
@nisheetpatel2654 Рік тому
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!
@user-xc5cx7lh4l
@user-xc5cx7lh4l 7 місяців тому
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.
@Shivnaren
@Shivnaren Рік тому
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
@bluesdog88
@bluesdog88 Рік тому
Arjan you are a strange man, that's mostly why I subscribed....keep up the good work and code!
@fexofenadinaGenerica
@fexofenadinaGenerica Рік тому
I agree. Type hints help to better understand the code.
@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.
@quaxBK
@quaxBK Рік тому
The strongest argument for type hints are for the LSP imo, you get much better diagnostics and completions that way.
@nargileh1
@nargileh1 Рік тому
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 5 місяців тому
Literally the primary reason I use them lol
@sillytechy
@sillytechy Рік тому
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.
@lowkeygaming4716
@lowkeygaming4716 Рік тому
Nice vid. Very insightful.
@ArjanCodes
@ArjanCodes Рік тому
Thank you, glad you liked the video!
@andreasgroth499
@andreasgroth499 Рік тому
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 Рік тому
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 Рік тому
​@@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.
@distant6606
@distant6606 Рік тому
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.
@colletantoine8885
@colletantoine8885 Рік тому
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.
@johnjsal
@johnjsal 7 місяців тому
Is that VS Code you were using? What is the name of that syntax theme?
@kayakMike1000
@kayakMike1000 Рік тому
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 Рік тому
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.
@bluzter
@bluzter Рік тому
Type hints are kinda satisfying which is why I love them.
@ivhea
@ivhea Рік тому
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 Рік тому
You got me!
@kosmonautofficial296
@kosmonautofficial296 Рік тому
Interesting video thanks
@Ludwighaffen1
@Ludwighaffen1 5 місяців тому
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. ;-)
@sync9827
@sync9827 Рік тому
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 6 місяців тому
Also, PyCharm hints become much better, when you're generous with adding type hints to your code.
@thomassmail6896
@thomassmail6896 Рік тому
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 Рік тому
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.
@rachinini
@rachinini Рік тому
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.
@willemvdk4886
@willemvdk4886 Рік тому
The Wilhelmscream had me, haha. And the fact that you used Uncle Bob's book lol
@ArjanCodes
@ArjanCodes Рік тому
Thanks :)
@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 11 місяців тому
You mean Optional[typing] ? lol
@riptorforever2
@riptorforever2 Рік тому
Psico introduction with Dexter's serial killer flavor.. loved!
@ArjanCodes
@ArjanCodes Рік тому
Tonight’s the night…
@Ziggity
@Ziggity Рік тому
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
@FabioKasper
@FabioKasper Рік тому
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. 😂
@zackadam2598
@zackadam2598 Рік тому
I liked this video explicitly for the LUNA dig. Spicy! 🌶
@andrewglick6279
@andrewglick6279 Рік тому
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.
@TheSzybas
@TheSzybas Рік тому
That "luhnatic" joke was perfect!
@ArjanCodes
@ArjanCodes Рік тому
Thank you Tomasz, glad you liked the video!
@cdhit
@cdhit Рік тому
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
@JuampyRabino
@JuampyRabino Рік тому
Love that you used Wilhelm's scream at the beginning 🤣🤣🤣🤣
@ArjanCodes
@ArjanCodes Рік тому
It's my favorite sound effect :)
@LewisCowles
@LewisCowles Рік тому
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 Рік тому
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 😁.
@sanjr3079
@sanjr3079 2 місяці тому
Finally, I'm convinced!
@ArjanCodes
@ArjanCodes 2 місяці тому
Glad the video was useful then! Ahah
@JeffreyChadwell
@JeffreyChadwell Рік тому
Nice use of the Wilhelm Scream!
@ArjanCodes
@ArjanCodes Рік тому
Ha, thanks! 😁
@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.
@Pawl0solidus
@Pawl0solidus Рік тому
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.
@josewilhelm2717
@josewilhelm2717 Рік тому
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 Рік тому
@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 Рік тому
@@HelloWorlds__JTS oh thanks, I didn't know. I will search it out!
@Celestialzzz
@Celestialzzz Рік тому
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?
@blutella4162
@blutella4162 Рік тому
Great video.
@ArjanCodes
@ArjanCodes Рік тому
Thank you!
@p.t.8312
@p.t.8312 Рік тому
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.
@samsung40_media87
@samsung40_media87 Рік тому
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
@handle_and_gretel
@handle_and_gretel Рік тому
use type hints and then vscode extension AutoDocString to create the small snippet for documentation
@airatvaliullin8420
@airatvaliullin8420 Рік тому
I don't want to push you in any particular direction *beheads a lego man who is against typehinting*
@TechieGuy82
@TechieGuy82 Рік тому
Love you videos.. Actually "your bad joke" made me laugh :D Thumbs up
@ArjanCodes
@ArjanCodes Рік тому
Thanks! 😃
@kurkabane702
@kurkabane702 Рік тому
Can someone explain why the function argument 'number' is reffered further as 'nr'? Is it some recent feature?
@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 ?
@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!
@sanketower
@sanketower Рік тому
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.
@Han-ve8uh
@Han-ve8uh Рік тому
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 Рік тому
> 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.
@HaiderGill-th5bp
@HaiderGill-th5bp 11 місяців тому
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...
@miguelvasquez9849
@miguelvasquez9849 Рік тому
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?
@matrixtoogood5601
@matrixtoogood5601 Рік тому
How to use type hints in frameworks like Django?
@cezhou9140
@cezhou9140 4 місяці тому
Type hints is like you spent an eternity to get accustomed to duck typing and then it gets shattered to dust.
@k98killer
@k98killer Рік тому
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.
@Betacak3
@Betacak3 Рік тому
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 Рік тому
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.
@majolakrem77
@majolakrem77 Місяць тому
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...
@JorgeEscobarMX
@JorgeEscobarMX Рік тому
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.
@TheKubaski
@TheKubaski Рік тому
I noticed that you use a tab to debug the python code. Does anyone know how to set that up?
@hshhsjhahsvs7728
@hshhsjhahsvs7728 Рік тому
Am rooting for type hints included.
@Maroonight
@Maroonight Рік тому
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 Рік тому
the problem is that is long? If yes you could probably define a short name for this.
@mesaber86
@mesaber86 Рік тому
Awesome intro :D
@ArjanCodes
@ArjanCodes Рік тому
Thanks MeSaber, happy you’re enjoying the content!
@presstv
@presstv Рік тому
Did you kill Person A along with Bernard The Headless? that's not fair🤣😂. great video Arjan :)
@willemvdk4886
@willemvdk4886 Рік тому
I definitely like "Type Driven Development" way more than I like "Test Driven Development", honestly ;)
@wilkyarny3012
@wilkyarny3012 Рік тому
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 Рік тому
If that happens, I would be very, very interested in that development.
@dennisvlahos
@dennisvlahos Рік тому
Typescript is statically typed though, which is something Python never will be
@tkerkvliet
@tkerkvliet Рік тому
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 Рік тому
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.
@hudabdulwahab2499
@hudabdulwahab2499 Рік тому
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 Рік тому
Use Optional from the typing module
@bitswapper
@bitswapper 9 місяців тому
VSCode doesn't show the type mismatch in python when I use type hints
@Andrumen01
@Andrumen01 Рік тому
Think about typehints as seat belts for code. In principle, they aren't needed, but during a crash they can save you!
@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
@Parisqiansen
@Parisqiansen Рік тому
Beware: If you don't write type hint, you LEGO body will be torn apart!
@DanielStoehr203
@DanielStoehr203 Рік тому
Do you have a favorite Type checker? Pyright? Mypy? And why? Are there differences?
@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).
@davidedigrande3204
@davidedigrande3204 Рік тому
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.
@edgeeffect
@edgeeffect 8 місяців тому
"I'm not telling you you must use type hits"... yes, but if I don't you'll dismember me and leave the remains of my body in a heap on you desk. :( ;)
@malmiteria
@malmiteria Рік тому
i think you didn't talk about some fair criticism of type hint tho. For exemple, one of the great value of type hint you talk about is entirely reliying on IDE tooling, which make it much less valuable in general (think any IDE that doesn't support type hint) Another big one to me is that it's a hint, so i can be wrong. And since it's built in a way for you to trust it, and rely on it, it can trap you in some errors hard to anticipate, if you absolutely type hint everything everytime. In other term, it's a situational tool, which has its good use case, and its dangerous use case also, for any non straightforward code, you're kinda lost in some more complexity I mean, how would you type hint a decorator for example? what about a decorator with parameters? Sure, it's doable, but is it really helping? I'm sure there's more to say, like, it's made to look a lot like type declaration in other statically type language, so it gives a very missplaced impression of what it is, for anyone coming from those language, which is definitely *not* going to lower the learning curve for them (by trapping them harder in the idea that it's more than just a hint i mentionned earlier, for example), and python is usually known for its great learning curve.
You Are Going To Like These New Features In Python 3.11
10:58
ArjanCodes
Переглядів 159 тис.
5 Good Python Habits
17:35
Indently
Переглядів 264 тис.
КИРПИЧ ОБ ГОЛОВУ #shorts
00:24
Паша Осадчий
Переглядів 2,9 млн
ВИРУСНЫЕ ВИДЕО / Мусорка 😂
00:34
Светлый Voiceover
Переглядів 8 млн
Зомби Апокалипсис  часть 1 🤯#shorts
00:29
INNA SERG
Переглядів 1,1 млн
15 Python Libraries You Should Know About
14:54
ArjanCodes
Переглядів 345 тис.
Why You Shouldn't Nest Your Code
8:30
CodeAesthetic
Переглядів 2,5 млн
PLEASE Use These 5 Python Decorators
20:12
Tech With Tim
Переглядів 77 тис.
Pydantic Tutorial • Solving Python's Biggest Problem
11:07
pixegami
Переглядів 225 тис.
5 Useful Dunder Methods In Python
16:10
Indently
Переглядів 41 тис.
Python 3.12 Generic Types Explained
18:27
ArjanCodes
Переглядів 55 тис.
25 nooby Python habits you need to ditch
9:12
mCoding
Переглядів 1,7 млн
5 Tips To Write Better Python Functions
15:59
Indently
Переглядів 60 тис.
5 Tips for Building Powerful Data Dashboards in Python
19:05
ArjanCodes
Переглядів 24 тис.
КИРПИЧ ОБ ГОЛОВУ #shorts
00:24
Паша Осадчий
Переглядів 2,9 млн