"TypedDict" In Python Is Actually AWESOME!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
how's it going guys in this video we're going to be talking about typed dictionaries and you can import that from the typing module in Python so why would we want to do that and what does it actually do well typed dictionaries brings types to dictionaries so if you want your code Editor to help you out with telling you that okay your dictionary is not conforming to the correct type well that's what type dictionary is going to do so for this example we're going to start off by creating a class called coordinate and I will indent that and it should inherit from type dictionary then inside a coordinate we always want to have an X and we always want to have a y and we might want to have a label as well of type string and a category so we'll type in category of type string and that's going to be how we want our coordinate to look right now if we were to create a coordinate and we were to use a dictionary to actually use those values and set X was going to be equal to 10 and not with a semicolon but with a colon and Y was to be equal to 10 as well you would see we would get no type hinting here so we could insert whatever we wanted and it would not conform to the coordinate that we created and that's bad because now if we insert this coordinate somewhere we're not going to have the required parameters so with a type dictionary we can now say that this coordinate is of type coordinate and you'll get some syntax highlighting immediately it's going to say you are missing these Keys label and category so now we can continue with our keys and this time you'll notice that the code editor is actually going to help us out by saying okay there's a label there so you can type in label and you can say who knows what we can say this is the profit plot so profit and we can insert the category the code editor now has a lot more context on what you're trying to do with that dictionary so category is going to take Finance so now we created a coordinate that has all the required Fields so we don't have any problems later but what happens if we do not want to include a field such as category but we still want the program to say that it's all right because maybe category is an optional field well let's duplicate this and we're going to call it coordinate two we're just going to change the values so we can see a difference we'll say 15 and maybe 25. the label is going to be set to expenses but this time we're going to exclude the category field and right now we're going to get that syntax highlighting because we need all of them for the code Editor to tell us that it's okay so if you want to have a field that is not required you're literally going to have to import from typing the not required type and with that you can go to category now and you can type in not required of type string so now it became an optional string that you can enter for category so that was a quick intro to using the typed dictionary but there are some cases that are going to give you a few issues because sometimes in a dictionary you might want to use a keyword that is reserved by the system such as for or in you might want to use these keywords and in Python how would you do that if it's already reserved by the system we don't want to give it any ugly names such as underscore 4 because that looks awful we want to actually use those so I'm going to be showing you the second way you can create typed dictionaries So Below this we can just remove all of that and we can create something called a vote this time which is going to be a typed dictionary and here we need to insert the class name or the type dictionary name and then the other fields it takes is a dictionary and it also takes a total which I will cover in just a moment but inside here immediately we can say 4 is going to be of type integer and against is going to be of type integer because we will have people who vote for something and people who vote against something but again 4 is reserved by python so we couldn't create a class with it and the total parameter defines whether all the values should be required and not by default it's set to true so the code editor will suggest that you need all of these values if you want to have a proper vote otherwise if you set this to false then it's going to say okay no matter what you insert it's going to be okay as long as it's one of these but just as with not required we have something that's called required and this all belongs to the type dictionary so if we actually duplicate this and we call this one vote 2. now we have a vote which will be vote 2 as well it has four against with total set two false but this time we want to have one of these fields required so here we can type in required and we will say that that's of type integer or of course if everything is required you can use the not required keyword the other way around if this is set to true so now let's try creating a vote so we have a vote of type votes and here we will insert D4 which will be 100 and against which will be at 250. and we will get no complaints by the code editor there but if we do omit one of these we're going to get some syntax highlighting if we have total set to True which it is by default but let's go to vote 2 so we'll say vote is of type vote 2. now even if total is set to false we suggested to the code editor that against should be required so here we can actually replace this with against and the code editor will say that is fine you insert it against that's the only one that's required so we will say that is fine but it does not work if we only have four because with total set two false we only required against and also if you were to type in something random such as that it would not pass even if we had nothing required here so if we said int you would see that if you pass in something that does not exist type dictionary will tell you what are you doing this is not what we agreed and it's also worth mentioning that before python 3.11 you were allowed to use syntax such as this I mean just by passing in the names explicitly this has been deprecated since python 3.11 so do not use this if you come across documentation that does use this and they're going to remove it in 3.13 so stick to the nice stuff which is using a dictionary inside the typed dictionary and I guess I didn't display what happens when you run this but it should be straightforward it's just a dictionary so if we print this vote we're going to get 4 100 against 100 again against 250. it's just a normal dictionary but now we have type hints for it which can make it a lot easier for us to debug later but I would love to hear what you think about this feature in Python do let me know in the comment section down below but with all that being said as always thanks for watching and I'll see you in the next video
Info
Channel: Indently
Views: 28,684
Rating: undefined out of 5
Keywords: pyton, pyhton, pythn
Id: XSkBFFOtQro
Channel Id: undefined
Length: 7min 32sec (452 seconds)
Published: Mon Jul 03 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.