"TypedDict" In Python Is Actually AWESOME!

  Рет қаралды 31,247

Indently

Indently

Күн бұрын

Пікірлер: 79
@tfr
@tfr Жыл бұрын
i love the way you pronounce “category”, very unique haha
@kamurashev
@kamurashev Жыл бұрын
I read it as …you pronounce category and unique. I listened the whole thing one more time to find out how does he pronounces “unique” and then I get back to this comment and actually read it correctly, so now I am laughing.
@man0utoftime
@man0utoftime Жыл бұрын
"ka-'TEG-o-ree" is a 1st for me, as well. 😂
@EverythingTechTime
@EverythingTechTime Жыл бұрын
Cet egery
@tribaltalker1608
@tribaltalker1608 Жыл бұрын
I had a student who pronounced "integer" as "in tee grr". He had never heard anyone say the word, so he guessed the pronunciation from reading it. English doesn't have much consistency in how we spell words as we have (mostly) spurned the accent marks that other languages use. It's a linguistic minefield.
@herrpez
@herrpez 9 ай бұрын
@@tribaltalker1608 It's been quite a while since English spelling and pronunciation drifted apart. But once upon a time they were actually fairly close. Add the fact that English is basically three languages in a trench coat... 😂 It's no wonder people get confused.
@kvelez
@kvelez Жыл бұрын
from typing import TypedDict, NotRequired class Coordinate(TypedDict): # inheritance x: float y: float label: str category: NotRequired[str] # could be excluded / type needed # IDE helps with type: Coordinate coordinate: Coordinate = {"x":10, "y":10, 'label':"Profit", 'category':"Finance"} coordinated: Coordinate = {"x":15, "y":25, 'label':"Expenses"} # category not used. 6:40 from typing import TypedDict, NotRequired, Required Vote = TypedDict('Vote', {"for":int, "against":int}, total=False) # override for Votes = TypedDict('Votes', {"for":int, "against":Required[int]}, total=False) # type needed vote : Vote = {"for":100, "against":250} votes : Votes = {"against": 450}
@kolterdyx
@kolterdyx 9 ай бұрын
Hell yeah! I had no idea this was possible! I've been missing this feature for ever since I started using typescript. Having an interface for a simple data type like a dictionary is the best!
@AlmondAxis987
@AlmondAxis987 Жыл бұрын
Ooh this is quite similar to c++'s 'typedef struct' declaration. It's super useful in data management for vectors and matrices.
@ajdziaj
@ajdziaj Жыл бұрын
Nice feature. I’m definitely going to try it out in next project. And I like your style of these videos, keep up the good work 🙂
@erickemayot9552
@erickemayot9552 Жыл бұрын
I really enjoy learning from your video compilations. Thanks so much for the good work. Keep them coming through.
@maswinkels
@maswinkels Жыл бұрын
Great video. Useful info and clearly explained. Thanks! (One small thing: the word 'category' has emphasis on the first syllable: CATegory).
@berkaybakacak
@berkaybakacak Жыл бұрын
This is not Python that I know 😂 This channel teached me a lot. Thank you so much
@SkyyySi
@SkyyySi Жыл бұрын
Please note that this is a bad use of TypedDict. The reason to use TypedDict is to adapt code that's already depending on untyped dictionaries to use types. Perhaps to add a type structure to the result of json.load(). However, designing your structured code from the ground up around dictionaries is a bad idea, because the purpose of a dict is to be unstructured. For designing strucutred types, Python has a dedicated mechanism: Classes. In particular, the dataclass from the dataclasses module. These are designed to have a structure, and are generally more convenient to use that way (for example, because they support looking up attributes with object.field instead of object["field"] out of the box).
@keithkaranu4258
@keithkaranu4258 Жыл бұрын
Not: I'm not a python guy so idk if this is a good practice in Python, but I can imagine this being useful for implementing algebraic data types sort of like interfaces in typescript.
@SkyyySi
@SkyyySi Жыл бұрын
@@keithkaranu4258 I don't see how using a dictionary with a fixed type structure would change the things you can model over a class
@youtubeenjoyer1743
@youtubeenjoyer1743 2 ай бұрын
Please note that this comment is wrong. There is not such a thing as ten commandments discouraging heterogenous dictionaries. The purpose of the dictionary data structure is to map keys to values. The attribute lookup syntax is irrelevant, except it is more limited in comparison to the key lookup syntax.
@matteolacki4533
@matteolacki4533 Жыл бұрын
Hello! Is it not against the idea of a dictionary to specify in advance the keys and their types? Python also has namedtuples that should be way more efficient in use, especially given that one needs to declare types in advance, so obviously one needs to know them in advance. Is there a version of namedtuples where one can specify types?
@paljain01
@paljain01 Жыл бұрын
I luv TypedDict, I had a long json body and keys were long I have to go back and forth to copy and paste that key. once I created the TypedDict the auto complete feels like magic ✨. unfortunately i had to use python 3.7 in prod so I had to install typing-extensions module to use TypedDict.
@rogumann838
@rogumann838 Жыл бұрын
I dont see why I would use this over creating a dataclass of some sort
@alansnyder8448
@alansnyder8448 8 ай бұрын
What version of python3 is this? The "typing" package seems to change between sub-versions. For example, I would have thought you would use Optional and didn't even know NotRequired existed.
@alidev425
@alidev425 Жыл бұрын
please make a video about class methods and object methods(all kind of it ) and the differences between them just for clarification (and also usage of them in real situations) because the way you teach is amazing and thank you very much for your consideration
@Indently
@Indently Жыл бұрын
I already did
@flames9310
@flames9310 Жыл бұрын
Thanks for the info! I feel like this will make OOP easier to a certain extent.
@thisoldproperty
@thisoldproperty Жыл бұрын
I'm now wondering when to use TypedDict vs @dataclass
@walker-gx1lx
@walker-gx1lx Жыл бұрын
same here...
@Skull0
@Skull0 Жыл бұрын
If you want your data structure to behave like a dict but with a typed schema to prevent error this is the solution, you'll have all the expected methods and behavior or a normal dict which differ from a object, even if the typing is only statically checked this is a good solution to that need. Otherwise you'll have to create a class that derives from dict and then add validation to ensure all parameters are of the right type, it will execute at runtime so add overload which may not be necessary compare to just let the developer know that the a parameter type is wrong at development time with static analysis.
@anasssofti9271
@anasssofti9271 Жыл бұрын
Emm This will definitely help to organise more the codes, thanks for the tip
@ada-victoria
@ada-victoria Жыл бұрын
Love your content! not sure if it's intentional but your channel logo is so similar to LinkedIn I keep thinking it's a video posted by them fyi...
@smyvensestime6995
@smyvensestime6995 Жыл бұрын
What code editor are you using? it looks very nice
@SalticHash
@SalticHash Жыл бұрын
Seams to be pycharm
@kunalsoni7681
@kunalsoni7681 Жыл бұрын
it seems very interesting to do 😀💯💙✨
@IndraKurniawan
@IndraKurniawan Жыл бұрын
Whoa. Bracket in Python!
@timelessjc
@timelessjc 5 ай бұрын
thanks for sharing. I tried this code: ====== from typing import TypedDict class Person(TypedDict): name: str age: int salary: float person: Person = { 'weight':125, 'height': '6ft', } print(person) ======= ... {'weight': 125, 'height': '6ft'} it worked fine and nothing stops me from giving anything I want to the 'person' dictionary. am I missing something here or I just don't get it?
@youtubeenjoyer1743
@youtubeenjoyer1743 2 ай бұрын
The TypedDict annotation is not enforced by the interpreter at runtime. If you want runtime type checking, you have to write it yourself by inspecting the Person class and comparing the dictionary with the declared type annotations.
@EW-mb1ih
@EW-mb1ih Жыл бұрын
What's the advantage of TypedDict vs dataclass?
@Alister222222
@Alister222222 Жыл бұрын
I am also wondering this. I guess one advantage might be that a Dict is (?) smaller and more lightweight than a class, since classes are kind of extended Dicts with a lot of extra functionality.
@blackdereker4023
@blackdereker4023 Жыл бұрын
@@Alister222222 I would put that under premature optimization. Unless you are creating hundreds of thousands of data or running Python on a microship you really shouldn't worry about that.
@spaghettiking653
@spaghettiking653 Жыл бұрын
@@Alister222222 What would be the difference with a slotted class, for example, memory-wise? If you make use of slots, I believe that dramatically shrinks the possible values, because the class no longer wraps a dictionary, but rather has distinct fields for the properties, exactly like the intended use of TypedDict?
@mntraders8258
@mntraders8258 6 ай бұрын
Thank you
@viktortodosijevic3270
@viktortodosijevic3270 Жыл бұрын
Why use this instead of a @dataclass?
@YatharthMathur
@YatharthMathur Жыл бұрын
Because dataclasses aren't an exact replacement of python native dictionaries. TypedDict is just a type that helps with IDE auto completion. It's not adding any additional functionality like dataclasses.
@Jason-b9t
@Jason-b9t Жыл бұрын
If you can implement your resource initialization yourself, then dataclass will indeed be better. But in most cases you will get data from other libraries, and these data structures are usually very "dirty". For example, processing from files (toml, json, xlsx), http/database responses, etc. are almost all nested dictionaries/lists. Converting these things into dataclasses will be cumbersome, creating a lot of dependencies and prone to errors .
@lordcasper3357
@lordcasper3357 Жыл бұрын
how and where to use this dictionaries?
@Indently
@Indently Жыл бұрын
They're just normal dictionaries, you use them exactly the same way you'd use other dictionaries.
@grippnault
@grippnault Жыл бұрын
I seem to not have Required and NotRequired in typing module. I'm using PyCharm
@grippnault
@grippnault Жыл бұрын
For me, I do not have Required and NotRequired. My version of Python (3.10.5) would only except Optional
@Indently
@Indently Жыл бұрын
You’re right, it’s new as of Python 3.11
@grippnault
@grippnault Жыл бұрын
@@Indently Thank you for replying.
@alex_brutforce
@alex_brutforce Жыл бұрын
I receive - ImportError: cannot import name 'NotRequired' from 'typing'. Python 3.9.5
@DrDeuteron
@DrDeuteron Жыл бұрын
ikr, a version minimum would be nice. I can't get the latest thanks to the security ppl.
@rishiraj2548
@rishiraj2548 Жыл бұрын
Good day greetings
@xzex2609
@xzex2609 Жыл бұрын
you realLY love typing :D , why would you even consider that the first coordinate = {...} has anything to do with that class Coordinate?
@albertashabaaheebwa9111
@albertashabaaheebwa9111 7 ай бұрын
Awesome. Although you didn't demostrate the use of reserved keywords since for != 'for', in != 'in' Nonetheless, Awesome tutorial
@darrenlefcoe
@darrenlefcoe Жыл бұрын
great video. It seems that python is chasing statically typed languages in this regard. Better to keep to core, simple, readable python...
@呀咧呀咧
@呀咧呀咧 Жыл бұрын
Thanks I learnt sth new
@papetoast
@papetoast 10 ай бұрын
One really annoying thing about TypedDict is that you can't inherit from a TypedDict and then change the type of the values already defined For example, this does not work: class A(TypedDict): x: int Class B(A): x: str
@codershorts
@codershorts Жыл бұрын
for was not acceptable in class but "for" is acceptable in class - for is reserved, not "for"
@falklumo
@falklumo 8 ай бұрын
So, a feature was introduced in 3.8, deprecated in 3.11 and to be removed in 3.13? Seriously? Is Python only for kids playing around?
@youtubeenjoyer1743
@youtubeenjoyer1743 2 ай бұрын
Python is a toy scripting language.
@tourdesource
@tourdesource Жыл бұрын
> Invent a dynamically typed language for quick-and-dirty scripting and glueing > Decide you need static types and end up with a language as messy as Ruby and as cumbersome as Java, except 90x slower > mUh eCoSyStEm
@DrDeuteron
@DrDeuteron Жыл бұрын
quick and not dirty. This fixation on typing is anti-pythonic.
@K9Megahertz
@K9Megahertz Жыл бұрын
This is why I just stick with C/C++. If you're going to write code, why use an interpreted language that's orders of magnitude slower than something else. In either case you're going to be writing a lot of code, so why gas up and drive a Pinto when you can do it with a Ferrari... Every program boils down to a few core concepts: loops, lists, arrays, variables, conditional statements, etc.. The part I don't understand is all these languages come out that try to be better/safer than C++ and then they just slowly all regress back towards C++.
@DrDeuteron
@DrDeuteron Жыл бұрын
@@K9Megahertz You are bastardizing Greenspuns 10th Law
@spaghettiking653
@spaghettiking653 Жыл бұрын
@@DrDeuteron I disagree, why not use types when your functions are required to take a specific type of data? Obviously some functions don't do this, e.g. print can print anything, but what about a function that iterates the lines of a file? Obviously it needs to take a file(-like) object, so why not type it as such? Then it's more obvious what the code does. And why not annotate what the function returns, because then you can tell without having to read the code? It's just easier, not to mention that the code editor can help you when it knows the possible types.
@DrDeuteron
@DrDeuteron Жыл бұрын
Sentinel pattern: iter(fp.readline, “”) Is sufficient
@rongarza9488
@rongarza9488 10 ай бұрын
So what language does the world pick for a lingua franca? English! hahahaha Want to know why? Because of the Industrial Revolution, and also the airline industry, and movies, and TV, and on and on. Moral of the story: you don't have to always be "right", you just have to be persistent and gain the leading edge. Don't ever give up.
@SirSX3
@SirSX3 Жыл бұрын
kategari? 😂
@TomLeg
@TomLeg 9 ай бұрын
English is a messed-up language ... "caaaaah-tegory'
@vishalprajapati1004
@vishalprajapati1004 Жыл бұрын
Please Hindi subtitle
@spaghettiking653
@spaghettiking653 Жыл бұрын
Sir I do not speak Hindi but you can try automatic subtitles and automatically translate to Hindi
@БогданЗинченко-к5п
@БогданЗинченко-к5п Жыл бұрын
pydantic with less abilities
@kutilkol
@kutilkol 9 ай бұрын
What a crap
How I Make My Successful Coding Shorts On YouTube
7:35
Indently
Рет қаралды 38 М.
Beat Ronaldo, Win $1,000,000
22:45
MrBeast
Рет қаралды 158 МЛН
Don’t Choose The Wrong Box 😱
00:41
Topper Guild
Рет қаралды 62 МЛН
Арыстанның айқасы, Тәуіржанның шайқасы!
25:51
QosLike / ҚосЛайк / Косылайық
Рет қаралды 700 М.
Glob Module in Python | How to Use Glob Module in Python
12:16
ADICHERLA ODELU
Рет қаралды 553
TypedDict is a LIFESAVER
6:44
Carberra
Рет қаралды 30 М.
5 Tips To Write Better Python Functions
15:59
Indently
Рет қаралды 114 М.
Every F-String Trick In Python Explained
19:43
Indently
Рет қаралды 30 М.
This Is Why Python Data Classes Are Awesome
22:19
ArjanCodes
Рет қаралды 819 М.
10 Ways You Can Use "_" In Python (Do you know ALL of them?)
9:56
Python dataclasses will save you HOURS, also featuring attrs
8:50
PLEASE Use These 5 Python Decorators
20:12
Tech With Tim
Рет қаралды 125 М.
5 Useful Python Decorators (ft. Carberra)
14:34
Indently
Рет қаралды 109 М.
Beat Ronaldo, Win $1,000,000
22:45
MrBeast
Рет қаралды 158 МЛН