this post was submitted on 15 Nov 2023
25 points (87.9% liked)

Python

6288 readers
5 users here now

Welcome to the Python community on the programming.dev Lemmy instance!

๐Ÿ“… Events

PastNovember 2023

October 2023

July 2023

August 2023

September 2023

๐Ÿ Python project:
๐Ÿ’“ Python Community:
โœจ Python Ecosystem:
๐ŸŒŒ Fediverse
Communities
Projects
Feeds

founded 1 year ago
MODERATORS
 

This is a discussion on Python's forums about adding something akin to a throws keyword in python.

you are viewing a single comment's thread
view the rest of the comments
[โ€“] [email protected] 1 points 11 months ago (1 children)

Anything but over9000 variations of nullables like in C#

[โ€“] [email protected] 1 points 11 months ago* (last edited 11 months ago)

I'm not too familiar with C# (last used it like a decade ago), but I think the rules here would be pretty simple:

  • x? - if x is None or an Error, return from the function early, otherwise use the value and continue
  • x?.y - same as above, but with an attribute of x
  • x ?? y - instead of returning as in the first, use y as the default value

And maybe add an option to convert exceptions from a function to an Error value (maybe some_func?() to convert to error values? IDK, I haven't thought through that part as much).

Hopefully that's simple enough to be useful.

If I were proposing this, I'd limit it to optional chaining since that's far more annoying to me currently.