Handling or Preventing Errors in Python: LBYL vs EAFP - Getting Started & Choosing a Style

  Рет қаралды 713

Real Python

Real Python

Күн бұрын

Пікірлер: 9
@vsolyomi
@vsolyomi 10 күн бұрын
*Shrek donkey picture*: Monads!
@vsolyomi
@vsolyomi 10 күн бұрын
In all seriousness, you're missing one critical thing here: what does you method/function return? In both If you just print, your func will implicitly return None which will propagate to the caller. So you just potentially gave yourself a NoneType exception somewhere up the call stack. And if it explodes far enough you might have no idea where it came from. I think this part is more critical than check vs raise argument. I hope the course gives it some consideration and hopefully at least touches conceptually on generic Result types and errors as values.
@lemonade2345-j4e
@lemonade2345-j4e 9 күн бұрын
can you point me to some article that deals with this issue in more elaborate way?
@vsolyomi
@vsolyomi 7 күн бұрын
​@@lemonade2345-j4ealternatively you can go the golang route and always return a tuple of (result, error) but it's about as un-pythonic as you can get. But maybe go read how it works in golang if you're interested, it's absolutely doable in python if you want to go this way
@vsolyomi
@vsolyomi 5 күн бұрын
@@lemonade2345-j4e not much for python specifically sadly, but in other places: rust uses Result types that are then matched on being Ok or Err, their docs are pretty decent. I never found a good Result implementation for python, but there is a Maybe implementation by Eran Kampf out there on github. Maybe is used similar to Result but only checks if something is or is not None. So it's kinda half way there but it's useful in it's own right (ever had to dig deep into nested json checking if every field along the way exist - yeah, no more with this one). If you're looking for better conceptual understanding and more elaborate examples... well, there's Functional Programming in Java by Pierre-Yves Saumont. If you can stomach a java book like that. Maybe there's something better out there, but for python there's like none.
@vsolyomi
@vsolyomi 5 күн бұрын
@@lemonade2345-j4e alternatively you can go the way of golang and always return a tuple of result error and check explicitly, but it's about as unpythonic as it gets I suppose. It's better to ask for forgiveness than permission though, so feel free to try and see. Both approaches - result wrappers and explicit return errors are absolutely doable in python and not even that cumbersome for most use cases, imo.
@vsolyomi
@vsolyomi 5 күн бұрын
@@lemonade2345-j4e one of my comments gets deleted or moderated or something. Anyways ArjanCodes recently made a video on Returns package, it kinda has all you asked for
@samoylov1973
@samoylov1973 10 күн бұрын
LBYL would be better, like this: >>> d = {1: "a"} >>> d.get(2, "there's no such key")
you will never ask about pointers again after watching this video
8:03
Quilt Challenge, No Skills, Just Luck#Funnyfamily #Partygames #Funny
00:32
Family Games Media
Рет қаралды 54 МЛН
Wall Rebound Challenge 🙈😱
00:34
Celine Dept
Рет қаралды 18 МЛН
Mom Hack for Cooking Solo with a Little One! 🍳👶
00:15
5-Minute Crafts HOUSE
Рет қаралды 22 МЛН
MIND-BLOWING BOX SHADOW Hover Hacks You Never Knew!
4:05
CatWebDev
Рет қаралды 124
Coding Interviews Be Like
5:31
Nicholas T.
Рет қаралды 6 МЛН
Working With TOML and Python: Data Types & Tables
23:00
Real Python
Рет қаралды 391
How do computers read code?
12:01
Frame of Essence
Рет қаралды 3,1 МЛН
Sudden assault near Kursk / Putin's statement
13:04
NEXTA Live
Рет қаралды 1,1 МЛН
I Made Sorting Algorithms Race Each Other
8:24
Green Code
Рет қаралды 238 М.
Quilt Challenge, No Skills, Just Luck#Funnyfamily #Partygames #Funny
00:32
Family Games Media
Рет қаралды 54 МЛН