'TypedDict' In Python Is Actually AWESOME!

preview_player
Показать описание
TypedDict in Python is actually awesome! In this video I will cover quickly how you can use it in your projects.

▶ Become job-ready with Python:

▶ Follow me on Instagram:

00:00 Intro
00:28 Using TypedDict
02:13 NotRequired
03:11 Problem
03:39 TypedDict alternative version
04:42 Required
06:36 Deprecated in 3.11
07:01 Printing the TypedDict
07:20 Summing it up
Рекомендации по теме
Комментарии
Автор

i love the way you pronounce “category”, very unique haha

tfr
Автор

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!

kolterdyx
Автор

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}

kvelez
Автор

Great video. Useful info and clearly explained. Thanks!
(One small thing: the word 'category' has emphasis on the first syllable: CATegory).

maswinkels
Автор

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 🙂

ajdziaj
Автор

Ooh this is quite similar to c++'s 'typedef struct' declaration. It's super useful in data management for vectors and matrices.

AlmondAxis
Автор

I really enjoy learning from your video compilations. Thanks so much for the good work. Keep them coming through.

erickemayot
Автор

This is not Python that I know 😂 This channel teached me a lot. Thank you so much

berkaybakacak
Автор

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?

matteolacki
Автор

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

alidev
Автор

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).

SkyyySi
Автор

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.

paljain
Автор

I dont see why I would use this over creating a dataclass of some sort

rogumann
Автор

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...

ada-victoria
Автор

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.

alansnyder
Автор

Thanks for the info! I feel like this will make OOP easier to a certain extent.

flames
Автор

Emm This will definitely help to organise more the codes, thanks for the tip

anasssofti
Автор

Awesome. Although you didn't demostrate the use of reserved keywords since for != 'for', in != 'in'
Nonetheless, Awesome tutorial

albertashabaaheebwa
Автор

I'm now wondering when to use TypedDict vs @dataclass

thisoldproperty
Автор

great video. It seems that python is chasing statically typed languages in this regard. Better to keep to core, simple, readable python...

darrenlefcoe