Python Has A 'Literal' Type That I've NEVER Seen Before

preview_player
Показать описание
This import from the typing module is incredibly useful when you need to check value types. Sorry about the bad word play in the title.

▶ Become job-ready with Python:

▶ Follow me on Instagram:

00:00 Intro
00:13 Writing the code
01:37 The problem
02:00 How to use Literal
03:31 Conclusion
Рекомендации по теме
Комментарии
Автор

I would love a video on Enum's. I haven't used them, but can really see their utility.

murphygreen
Автор

I think python's open function doesn't use that because it supports combinations (r/w/x/a plus optional +, plus optional t/b, for example r+b)

Issam-app
Автор

In VS Code, whenever I name a variable in capital letters (eg. PAGINATE_BY) and assign a value to it, if I hover over the variable, it says that it has a type of Literal. Always wondered what it meant, but understood it now.

steadyfall
Автор

I think I saw that in the Pillow Lib, they use the Literal to give possible color modes when creating an image object. I always wondered how this was implemented.
However, other languages would have used enums here.

Kommentierer
Автор

Another great video from you about another feature I did not know existed. I probably would have used an Enum here, but in small cases like this, this is much easier.

vorpal
Автор

What is the benefit of using this over something like an enum?

pharoah
Автор

So does python care, or is it just for the IDE.?

DrDeuteron
Автор

This is a bit off-topic for this video, but I was trying to come up with an example using Enums for someone below, and I realized that this code doesn't work:

Language = NewType('Language', str)
english = Language('English')
isinstance(english, Language)

Python doesn't recognize Language as a type, at least in 3.11.1.

You have to use:
isinstance(english, str)
to get it to work. Rather disappointing.

vorpal
Автор

What purpose does TypeAlias have in the code with Literal[args]? The hint doesn't make sense to me here.

niistrai
Автор

1:00 jetbrains pycharm has ligatures on by default?

yash
Автор

What if you hardcode the "open" module and add literals?

andresbonelli
Автор

Vscode hint shows literal suggestions all the time

tonussi
Автор

Another nice explanation. Thank you ☺

castlecodersltd
Автор

In fact Python does implement the 'mode' parameter in 'open' function with Literal. The reason why there lies no warning probably is that the IDE is not smart enough.😂

glut
Автор

Seems like this merits an Enum instead of a Literal.

vorpal
Автор

Bro remember me?
Btw a recomendation for video: bro is there a way to set application as desktop screan background for python or litreally any application Please tell

arnavsingh
Автор

i think its too useless. its only for code editor
it will work fine even if you make wrong type

sootguy
Автор

Typing, typing, typing...recently just videos that presents code on aiding pycharm lynting warnings, if you need 100 lines of code just to aid your IDE why bother, instead rather make videos how to handle user input errors like in backend, where things can really go south, not how to aid yourself in writing your own code

Sinke_