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!
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 21

  • @mmoussa01
    @mmoussa01 3 ปีที่แล้ว +9

    "from typing import Optional as Noneable" 😁

  • @WaylonWalker
    @WaylonWalker 2 ปีที่แล้ว +1

    I like when you keep the stream cam setup

  • @ng3147
    @ng3147 11 หลายเดือนก่อน

    exactly the explanation i needed. thank you!

  • @shubhamjoshi6826
    @shubhamjoshi6826 3 ปีที่แล้ว +2

    thank you Anthony 😊

  • @srkbhayo
    @srkbhayo 2 ปีที่แล้ว

    thank you . I was sos confused with this keyword

  • @realabja1
    @realabja1 3 ปีที่แล้ว +1

    You awesome man🚀

  • @Strider817
    @Strider817 3 ปีที่แล้ว +3

    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?

    • @anthonywritescode
      @anthonywritescode  3 ปีที่แล้ว +1

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

  • @sadhlife
    @sadhlife 3 ปีที่แล้ว +3

    that new | syntax has already landed in 3.10 apparently

    • @anthonywritescode
      @anthonywritescode  3 ปีที่แล้ว +2

      indeed! I've got it in my copy of 3.10a2!
      >>> int | None
      int | None

    • @sadhlife
      @sadhlife 3 ปีที่แล้ว +1

      @@anthonywritescode noice

  • @brianfontenot9925
    @brianfontenot9925 2 ปีที่แล้ว +2

    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

  • @MatthewPherigo
    @MatthewPherigo ปีที่แล้ว

    In NixOS types, this is called "NullOr", maybe more clear.

  • @goodclover
    @goodclover 3 ปีที่แล้ว +1

    30:32 I'm sure there's plenty of other thing that you would want to change as well. 😅

    • @retrom
      @retrom 3 ปีที่แล้ว +3

      damn, which dimension is 30minutes into a 4minute video

    • @goodclover
      @goodclover 3 ปีที่แล้ว

      @@retrom I have no idea what happened there.

  • @nexovec
    @nexovec ปีที่แล้ว

    Even Option[int] would be way better I don't get it...

  • @shivanshmishra5293
    @shivanshmishra5293 3 ปีที่แล้ว +1

    Trying to replicate maybe monad I guess.

    • @anthonywritescode
      @anthonywritescode  3 ปีที่แล้ว +1

      I mean, it's clear what it is -- just the name is bad :)

  • @MElixirDNB
    @MElixirDNB ปีที่แล้ว

    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