python typing: Optional is not optional! (intermediate) anthony explains
ฝัง
- เผยแพร่เมื่อ 14 พ.ย. 2020
- today I talk about the Optional type in python typing and why I think it is very poorly named
playlist: • anthony explains
==========
twitch: / anthonywritescode
dicsord: / discord
twitter: / codewithanthony
github: github.com/asottile
stream github: github.com/anthonywritescode
I won't ask for subscriptions / likes / comments in videos but it really helps the channel. If you have any suggestions or things you'd like to see please comment below! - วิทยาศาสตร์และเทคโนโลยี
"from typing import Optional as Noneable" 😁
too true!
I like when you keep the stream cam setup
exactly the explanation i needed. thank you!
thank you Anthony 😊
thank you . I was sos confused with this keyword
You awesome man🚀
I'm one of those people confused by this naming, and your video helped quite a bit in explaining stuff. However, I still don't understand why you can't set an optional function argument to Optional[int], since it seems like Optional means the type could be either Int or None. In your function y is an int when set by default, so why is it not valid to do this?
you can use Optional[int] to annotate a function argument, however it has a different meaning than "this argument is optional" -- it actually means something closer to "this argument is None-able (nullable, but translated to pythonspeak)"
that new | syntax has already landed in 3.10 apparently
indeed! I've got it in my copy of 3.10a2!
>>> int | None
int | None
@@anthonywritescode noice
Optional is really a type that means we have a box and inside the box maybe there is some(value) or none(value). Its a great tool if enforced and completely eliminates the runtime nil/null reference at “compile” time. If incorporated into the language correctly
In NixOS types, this is called "NullOr", maybe more clear.
30:32 I'm sure there's plenty of other thing that you would want to change as well. 😅
damn, which dimension is 30minutes into a 4minute video
@@retrom I have no idea what happened there.
Even Option[int] would be way better I don't get it...
Trying to replicate maybe monad I guess.
I mean, it's clear what it is -- just the name is bad :)
OrNone might have been more intuitive. but now we can just use the easier | None syntax.. although lots of work environments are stuck on older python versions so pain ensues