• richieadler@lemmy.myserv.one
    link
    fedilink
    English
    arrow-up
    5
    ·
    1 year ago

    What happens in other languages you use when you try to access a non-existing key for a hash/map/dict?

    What language do you use that accessing an object attribute is the same that accessing a dict key?

    What knowledge do you have (or not) that KeyError is a mistery to you?

    • Hallainzil@lemmy.world
      link
      fedilink
      arrow-up
      3
      ·
      1 year ago

      What language do you use that accessing an object attribute is the same that accessing a dict key?

      Javascript / Typescript.

    • cocobean
      link
      fedilink
      English
      arrow-up
      1
      ·
      1 year ago

      Return undefined.

      Typescript.

      Why error? Just return undefined. Simple, no try/catch needed.

      • richieadler@lemmy.myserv.one
        link
        fedilink
        English
        arrow-up
        1
        ·
        1 year ago

        Because that’s prone to errors. And the Zen of Python includes “explicit is better than implicit” and “Errors should never pass silently”. Languages that do otherwise create bad habits.

        • cocobean
          link
          fedilink
          English
          arrow-up
          1
          arrow-down
          1
          ·
          1 year ago

          Is it? It’s just an optional property. And Typescript will tell you that it’s optional.