Тёмный

Monadic Error Handling in Python ⚠ Write Better Python Code Part 7B 

ArjanCodes
Подписаться 239 тыс.
Просмотров 47 тыс.
50% 1

This video is a follow-up from last week's video, where I focus on a particularly interesting error handling mechanism called monadic error handling. Monadic error handling stems from functional programming and it has some advantages over using traditional exceptions. Next to talking about monadic error handling, I also address two issues from last week's video. If you haven't watched that video yet, I suggest you do that before watching this one: • Exception Handling Tip... .
💡Here's my FREE 7-step guide to help you consistently design great software: arjancodes.com/designguide.
Relevant books and links:
- Design Patterns: Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides: amzn.to/3jllgyH
- Applying UML and Patterns: An Introduction to Object-Oriented Analysis and Design and Iterative Development, by Craig Larman: amzn.to/364wgLb
- Principles of Package Design: Creating Reusable Software Components by Matthias Noback: amzn.to/2NETK3l
- Clean Code: A Handbook of Agile Software Craftsmanship by Robert Martin: amzn.to/3qVZgNs
- The original Design Principles and Design Patterns article by Robert Martin: fi.ort.edu.uy/innovaportal/fi...
- All code samples from this Python series can be found here: github.com/arjancodes/betterp...
- The dry-python libraries: dry-python.org/
My gear:
📷 Sony A7C - amzn.to/3aShCtw
🎙 Rode Wireless GO - amzn.to/3pSLFVU
💡 GVM RGB LED light - amzn.to/3qRNJid
💡 GVM 100W light with lantern softbox - amzn.to/2NuU8lk
All parts in this series:
Part 1: Cohesion and coupling - • Cohesion and Coupling:...
Part 2: Dependency inversion - • Dependency Inversion: ...
Part 3: The strategy pattern - • The Strategy Pattern: ...
Part 4: The observer pattern - • Observer Pattern Tutor...
Part 5: Unit testing and code coverage - • 100% CODE COVERAGE - T...
Part 6: Template method and bridge - • Two UNDERRATED Design ...
Part 7a: Exception handling - • Exception Handling Tip...
Part 7b: Monadic error handling - • Monadic Error Handling...
Part 8: Software architecture - • Why You Should Think A...
Part 9: SOLID principles - • Uncle Bob’s SOLID Prin...
Part 10: Object creation patterns - • QUESTIONABLE Object Cr...
🔖 Chapters:
0:00 Intro
1:32 Addressing a few issues from the previous video
3:34 What is monadic error handling?
6:26 Code example
11:44 Final thoughts
12:44 Outro
👍 If you enjoyed this content, give this video a like. If you want to watch more of my upcoming videos, consider subscribing to my channel!
🏆 Join my Discord server: discord.arjan.codes
👥Twitter: / arjancodes
👥LinkedIn: / arjancodes
👥Facebook: / arjancodes
#arjancodes #softwaredesign #exceptions
DISCLAIMER - The links in this description might be affiliate links. If you purchase a product or service through one of those links, I may receive a small commission. There is no additional charge to you. Thanks for supporting my channel so I can continue to provide you with free content each week!

Опубликовано:

 

1 июл 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 119   
@ArjanCodes
@ArjanCodes 3 года назад
Let me know if you'd like me to do a more in-depth video about monads and monadic error handling - I only touched the surface here ;).
@johnflannery7145
@johnflannery7145 3 года назад
I would love a video exploring monads further!
@ArjanCodes
@ArjanCodes 3 года назад
Thanks John - will do!
@ColinHoernig
@ColinHoernig 3 года назад
Thank you for this great content. Please do create more videos like these. I am excited to try this approach out of monadic error handling.
@elefantsnablar
@elefantsnablar 3 года назад
Yes please!
@Victorinoeng
@Victorinoeng 2 года назад
Yes! Very interested. I mentioned in a previous video and would like to share my opinion again: would love to learn more about FP in Python. Especially with your way of teaching, comparing the conceptual pros and cons, as well as implementation example. Makes it so much easier to have things “click”! Great work, Arjan. Awesome attitude towards recognizing and learning from mistakes! Also made it clear you do read and care about comments! ;)
@sagnikbhattacharya1202
@sagnikbhattacharya1202 2 года назад
I am telling you, everyone will love it when Python slowly evolves into Haskell.
@mathmo
@mathmo 2 года назад
The python maintainers could start by adding tail recursion to CPython
@torurz
@torurz 2 года назад
Starts addressing mistakes in previous video. Proceeds to talk about monads. You’re a brave man!
@cristobaljvp
@cristobaljvp 3 года назад
I've been studying Python (and programming in general) for like 8 months now, and after some months where I didn't feel like I learned a lot stumbling upon this channel has feel like a blessing. I've binged a lot of your videos and I feel like I've learnt so much in such a short time, I can't imagine how much more I can learn with 1 or 2 more years. It's insane for me to think the amount of knowledge seniors like you must have. I hope I can be like you in a few years. Thanks for the content!
@ArjanCodes
@ArjanCodes 3 года назад
Thank you very much! I’m very happy the videos are helping you.
@rawallon
@rawallon 2 года назад
Have you foound another channel like this one? I just got the "Fluent Python" book
@Just2Dimes
@Just2Dimes 2 года назад
@@rawallon check out the mcoding channel. It has some good intermediate to advanced python content. Arjan's channel is nr 1 when it comes to software design I'd say though.
@AdrianBridgett
@AdrianBridgett 2 года назад
Thoroughly enjoying these. The level is perfect for me - starts out simple with familiar concepts, then moves into the meat of the topic - explaining not just what, but the all important _why_ with clear examples (including what's wrong with the alternatives). Then as a bonus often introducing more advanced areas. Top marks :-) Love the humour as well as the human and compassionate side. Thank you Arjan!
@CoderShark
@CoderShark Год назад
Love from my heart, Brother I have learned so many advanced and simple but precise concept from you videos which I had never learned in my 4.5+ year of python development. Please keep it up.
@ArjanCodes
@ArjanCodes Год назад
Glad you like the content!
@manpreetkhokhar5318
@manpreetkhokhar5318 3 года назад
Loving this series ...Thanks Arjan...Keep making such quality stuff 👍
@ArjanCodes
@ArjanCodes 3 года назад
Thank you Manpreet, will do!
@redred582
@redred582 2 года назад
i can say this very good channel, not like others tutorial/something.. its teach me writing code with better structure.. not many channel has good programing content like this.. thanks arjan
@ArjanCodes
@ArjanCodes 2 года назад
Thank you! I’m happy you’re enjoying the content!
@jonathanheadley2729
@jonathanheadley2729 2 года назад
Recently stumbled across your videos and i'm really enjoying your content! thank you!
@ArjanCodes
@ArjanCodes 2 года назад
Glad you enjoy it, Jonathan!
@MrTrebor2
@MrTrebor2 2 года назад
Yes please more videos about this way error handling. Maybe some intro to functional programming too. What are pros and cons of FP - testing ? parallel computing ?
@elefantsnablar
@elefantsnablar 3 года назад
Just learnt about ROP in F#, interesting to see the pattern applied in Python too. Great video!
@ArjanCodes
@ArjanCodes 3 года назад
Thanks Axelius, glad you liked it!
@Baroquepassion
@Baroquepassion 3 года назад
very interesting, I'm comparing it to things like CompleteableFuture in Java or reactive approaches. Nice to see this kind of thing in Python.
@ArjanCodes
@ArjanCodes 3 года назад
Thank you Matthew, I’ll definitely revisit this topic in the future. Possibly when Python 3.10 is out because structural pattern matching, new union type syntax and monadic error handling are a match made in heaven 😊.
@domingochavez14
@domingochavez14 2 года назад
Very good content! I've been programming in Python for several years and I started learning Haskell early this year. I ended up adapting Haskell's "Maybe" logic into my Python programs to handle errors as results. I'm glad to see that I'm not alone and that this now part of a library. Will for sure take a look into it. Thanks!
@Sousleek
@Sousleek 2 года назад
what benefits it gave to you? btw returns is not a stdlib
@joaopedrorocha5693
@joaopedrorocha5693 4 месяца назад
Great video! It would be great to see it updated to use structural pattern matching to see how it can be used to achieve it.
2 года назад
This sounds very similar to Go's `data, ok = called_function`. A very interesting way to do error handling in a more obvious way than exceptions.
@tajmeet
@tajmeet 2 года назад
Exactly, I was thinking the same. Go's approach to error handling is very good in my personal opinion. But since you can't really return Exception type in python, I was thinking that we may need to raise it in the place where your function is being called. Similar to Go's res, err := your_function() If err { // Something here } Which would be something like this in python res, err = your_function() try: ... if err: raise YourCustomException except YourCustomException: # handle that here Although, there may be better ways of doing Go's style of error handling in python. Any inputs on this @ArjanCodes ?
@Daniel_Zhu_a6f
@Daniel_Zhu_a6f 2 года назад
@@tajmeet the problem is that python functions already handle errors with throwing mechanism. error monad catches the exceptions that were raised inside a function and passes them through. Your code requires function to return exception, which is not the case for most of the libraries.
@julians.2597
@julians.2597 3 месяца назад
​@@Daniel_Zhu_a6f not really a problem. Either 1) you don't know that an external function can throw, in which case you wouldn't have written a try/except for it anyways and you lose or gain nothing. 2) you just wrap it to return a Result monad instead.
@Daniel_Zhu_a6f
@Daniel_Zhu_a6f 3 месяца назад
@@julians.2597 it is a problem, because, realistically, almost any function can throw an exception. it is stupid to wrap any function, and python does not have macros. it is especially stupid, considering that the primary applications of python are shell scripts and data-processing notebooks -- they don't need to be infallible. i've come to the conclusion that monadic error handling matters only for a compiled language. seems like many people who create functional scripting languages came to the same conclusion (eg julia, clojure) and went with an exception system, rather than monadic one.
@nicwanavit5463
@nicwanavit5463 3 года назад
cool i always do this in swift and didnt know theres such a nice library. uve just made my day
@ArjanCodes
@ArjanCodes 3 года назад
Thank you Nic, glad you liked it!
@ke30_
@ke30_ 3 года назад
The past couple weeks I've updated lots of my apps to using the Django ORM, and yes, life is good now.
@ArjanCodes
@ArjanCodes 3 года назад
Indeed 😊
@10crack8
@10crack8 3 года назад
A good video would be about Structural Pattern Matching in Python.
@ArjanCodes
@ArjanCodes 3 года назад
Yes, good suggestion that I’ll keep in mind once the new version of Python is out.
@elatedbento
@elatedbento 9 месяцев назад
@@ArjanCodes Hi Arjan. It would be great if you could revisit this with match statements. You said it would simplify the example, but I'd like to see this example again w/ pattern matching. I understand it might be a lot of work, so even a small short would be nice. ♥
@EW-mb1ih
@EW-mb1ih 2 года назад
I don't really get the advantage of using monadic error handling but maybe that's because I am not sufficiently advanced yet. I know that it exists and maybe the usefulness of this way of handling error will become clearer in the future
@AlexanderWhillas
@AlexanderWhillas 2 года назад
oh oh, returns is a cool library. I'm going to use it to make a DSL parser that I have been having trouble with error handling!
@fuuman5
@fuuman5 3 года назад
Haha, this is crazy. This video is just like a look back to my time at university. Back then there was a module called "functional programming" and it was my second semester I believe. We programmed in Haskell and talked about Monads and I invested so much time to understand that concept and in the end I was not even close. It was way too much for me at this time. Cool memories :D
@ArjanCodes
@ArjanCodes 3 года назад
Always happy to provide, @fuuman5 😊
@vikingthedude
@vikingthedude Год назад
May I ask which Uni and degree you took? Most curriculums I've heard of would be composed mainly of OS, networking, compilers, OOP etc. Haven't heard of Haskell being taught let alone FP, and that too in just the second semester! Sounds like a good programme
@edgeeffect
@edgeeffect 2 года назад
I've heard Haskell people go on about Monads for years.... so simple when you just describe them without all the Haskell "look at me... I'm very clever" and we just say "oh yeah... just like error handling in Rust or promise resolution in JavaScript".
@vytautasusaitis
@vytautasusaitis 3 года назад
This is very interesting and useful. Your videos inspire me to go back and change my noobie code I keep writing. However, this raises a good topic for a future video - "When is code good enough?". At some point you shouldn't go back and keep optimising the code and should probably focus on something new, right?
@ArjanCodes
@ArjanCodes 3 года назад
Indeed, at some point you have to accept some imperfections in order to be able to release something. In a company, you have deadlines that force you to not focus too much on perfection. For your own projects, it also helps to define deadlines for yourself to make sure you don’t get lost in the details. In the end, perfect code doesn’t matter if it’s never released to be used by anyone.
@MrCucolea
@MrCucolea 3 года назад
Great content as always! Covering SQLAlchemy would be a great topic.
@ArjanCodes
@ArjanCodes 3 года назад
Thanks and great suggestion!
@Madeinoz1967
@Madeinoz1967 3 года назад
+1…..With async SQLite
@FannyVanderbildt
@FannyVanderbildt 8 месяцев назад
@ArjanCodes , first of all thank u for all the content ❤ secondly, i agree, mistakes happen and it is not a big deal that u have made a mistake. Thirdly i think that editing the previous video could be a good idea cause i guess many ppl treat you as an authority and might be mislead. Again, Thanks for the effort to provide a great content
@edgeeffect
@edgeeffect 9 месяцев назад
Monadic error handling is so much better than exceptions. Exceptions give you 2 separate paths of control which adds to confusion. Monads offer just one simple path of control. If only FP advocates could stop all that pretentious gobbledegook about "monoids" and "endofunctors" more people might see past the obfuscation and see that it's a better pattern. Count on Uncle Arjan to make things simple.
@PawelOlas
@PawelOlas 3 года назад
this is by far the best example of using dry-python I have seen so far. Thank you. Most of the examples I have seen on-line seem forced and imho make the code less readable and maintainable. Keep the videos coming :-)
@ArjanCodes
@ArjanCodes 3 года назад
Thank you very much & will do ;)
@the1gofer
@the1gofer 2 года назад
sure, I'd be interested in monads and monadic.
@robertbrummayer4908
@robertbrummayer4908 2 года назад
Great video
@alberto.cartaxo
@alberto.cartaxo 2 года назад
Great video!! I wonder if this pattern would be good design patternn for a pandas function that does a lot of data transformation.
@ArjanCodes
@ArjanCodes 2 года назад
Thank you Alberto. I have to look more into pandas. But I can imagine if you have some sort of pipeline that this kind of error handling would fit in quite nicely.
@jean4j_
@jean4j_ 18 дней назад
I love this library. However when using the @safe decorator with classes methods, MyPy doesn't get it right. It doesn't seem to do well with the first "self" parameter of class methods. That's a shame.
@johncrunk8038
@johncrunk8038 2 года назад
You make my brain hurt! But "no pain, no gain", I guess. Thanks for the great series of videos; keep pushing us.
@ArjanCodes
@ArjanCodes 2 года назад
Thanks John, glad you're enjoying the videos!
@zacharythatcher7328
@zacharythatcher7328 2 года назад
I used to always use monadic error handling when I programmed in C, and something always bothered me about exception handlers in OOP. I always got the feeling that there was some easy and common (but unspoken) way to structure my error handlers in a foolproof way that prevented hidden control flows (and encouraged meaningful error messages!) that I just wasn’t seeing, but even if I put in the time to develop these habits, it would be impossible to get everyone on the same page because of open-source. Also… I just never had proper incentive. That’s the kind of thing that a typical manager will hate you for spending time on. I really appreciate you making me aware of this easy to implement/check pattern!
@gregoryzhang743
@gregoryzhang743 3 года назад
great video !
@ArjanCodes
@ArjanCodes 3 года назад
Thank you Gregory, happy you enjoyed the video.
@MarkusBurrer
@MarkusBurrer 9 месяцев назад
I would love to see Rusts Option and Result Types and F# pipe operator in more languages
@yudhiesh1997
@yudhiesh1997 2 года назад
Cool this is just like how Rust does its error handling.
@nicholashoo4080
@nicholashoo4080 Год назад
Thanks for the info. This is what the internet is for!!!!! Not posting silly dance moves.
@ArjanCodes
@ArjanCodes Год назад
Thank you Nicholas, glad you liked the video!
@jakefischer8281
@jakefischer8281 2 года назад
You the man!
@ArjanCodes
@ArjanCodes 2 года назад
Glad you liked the video!
@Sousleek
@Sousleek 2 года назад
How to get know which nested function caused the exception to perform recover action or to form propper log statement? Say i called function that called function that called function which gave a KayError. How do i understand from which of them i got this failure at the beginning of the chain? A CustomExceptions comes to mind bot how is this woult be better than to use same custom exception in normal way instead of except Exception? a try/except does not obligates to mandatory use of flow() to start application but direct passing of exceptions do. In the end our code just gets some rigidity and no more benefits.
@Sousleek
@Sousleek 2 года назад
also async results imo are mess in this library
@RobertSiemer
@RobertSiemer 2 года назад
If __enter__() raises an exception, __exit__() will not be executed. Meaning: your minor change in 6:36 makes no sense.-A context manager guards exceptions once it is set up, it can not guard it own setup before being ready to do so.
@BhattVihang
@BhattVihang Год назад
How to log error that is printed on a screen in a log file without using try except construct.
@internetimagery
@internetimagery 2 года назад
One cool feature with monadic error handling, is you get to change the standard "programmer errors lumped in with real application errors" situation around. Now, if you catch all known errors correctly (instead of using the safe decorator) and pass them back as explicit Failure states, you can pass around issues the application can choose to handle (or not). Any further errors you get will bubble up, in a similar fashion to go/rust panics, which you can let reach the surface. This means the NameError example FINALLY gets to be treated differently to a database connection issue, etc.
@simonwang5800
@simonwang5800 2 года назад
Yes, deeper please
@MrKostrom
@MrKostrom 2 года назад
Isn't this similar to how python already handles exceptions, under the covers, and why it's good with exceptions but bad with recursion? Is there an advantage to doing it explicitly, or is it just "another option"?
@ArjanCodes
@ArjanCodes 2 года назад
The main difference between the traditional exceptions and monadic error handling is that traditional exceptions create a second control flow and stop executing the rest of the code until you a) handle the exception or b) stop the program. Having this second flow can lead to problems because you can no longer clearly see from the code how an error affects the rest of the program, unless you explicitly specify that using try/except. With monadic error handling, there is no second control flow, it's an explicit part of your regular code (via the Result object). The idea is that this makes it easier for developers to understand what happens when an error occurs, and which parts of the code are affected by it.
@al.e.k
@al.e.k Год назад
@@ArjanCodes Very interesting topic! What kind of error handling do you use in your personal projects? How do you choose between these two types of error handling?
@gaxkiller
@gaxkiller 6 месяцев назад
The more I use python (I have to use it at work), the more I see its flaws and I don't understand why people choose it for big api projects. I know it is a very important if not the most important in ML and Data fields, but for doing other stuff I see so many other languages that fit better... It's a shame people get stuck with the last language they use and are not willing to switch, and since ML is so important nowdays, we see python used more and more where IMHO it should not
@uuuummm9
@uuuummm9 5 месяцев назад
I did not get it. We can achieve the same with regular exception handling if we just try catch in the top level function and return a result depending on the error. As a plus we will have a stack trace for the cases when it should be investigated by someone.
@joshuaworman4022
@joshuaworman4022 Год назад
am i high or did someone just explain monads in a way that wasnt terrible. good job.
@ArjanCodes
@ArjanCodes Год назад
Thanks Joshua!
@technicholy1299
@technicholy1299 Год назад
Kotlin has a pattern like this with Sealed classes.
@MrWorshipMe
@MrWorshipMe 3 года назад
I just thought of something which might be stupid, but i'd like to know what's wrong with it: Instead of using monads or exceptions, why not have dependency injection of error handling callbacks? This way you make sure that an element can't be created without considering all it's failure states, it's less costrly in terms of runtime, and you don't need a big switch case or if else block to handle lots of differen't errors at the call site. It's much easier to ensure the error is handled as soon as possible and doesn't introduce any dependency. Why doesn't anyone use this pattern?
@ArjanCodes
@ArjanCodes 3 года назад
That's actually how it was done for a long time in JavaScript. This approach commonly resulted in the so-called "callback hell". That occurs when you need to do other things in the error callback function that themselves might also have a callback. And if callbacks are the default way of handling errors, this will be quite common. End result: a chain of callbacks that results in a really complicated control flow that's hard to debug and design around.
@MrWorshipMe
@MrWorshipMe 3 года назад
@@ArjanCodes makes sense. I guess it's an example of over using dependency injection.
2 года назад
Yes, callbacks end up being a nightmare. Now people use promises to fix that problem, and those are somewhat similar to monads in that they pass around wrapper objects with extra information.
@NoahBirnel
@NoahBirnel 7 месяцев назад
The dry-python link is broken.
@Norhther
@Norhther 2 года назад
Check merry for exception handling :D
@ArjanCodes
@ArjanCodes 2 года назад
Interesting, thanks!
@vlplbl85
@vlplbl85 3 года назад
I hate to use ... except Exception as e: print(e). What I do is import traceback ... except: traceback.print_exc() This way I can see the full error path and not just the final error. Otherwise you can see something like "None value passed while expecting a string" and I wonder where that came from
@darsh_shukla
@darsh_shukla 3 года назад
I use this technique a lot but don't know the name of technique.
@ArjanCodes
@ArjanCodes 3 года назад
Nice! Do you use the dry-python library as well, or something else?
@randixlai9216
@randixlai9216 9 месяцев назад
It looks like Go.
@lucacillario95
@lucacillario95 2 года назад
Nice video, but to me doesn't make any sense to use this in a dynamically typed language like python.
@sachinkainth9508
@sachinkainth9508 Год назад
I've seen methods in Python of the form __something__, is there any special significance to the double underscores?
@MauroScomparin
@MauroScomparin 2 года назад
Really ugly to use monads in python this way...
@edomeindertsma6669
@edomeindertsma6669 2 года назад
Write docstrings please.
@ArjanCodes
@ArjanCodes 2 года назад
Normally that’s a nice practice. I tried it in my video examples but it doesn’t work since they detract from the code and lead to more scrolling on screen. So I’ve stopped adding documentation and instead focus on making sure the code and the naming is as simple and clear as possible.
@edomeindertsma6669
@edomeindertsma6669 2 года назад
@ArjanCodes, I don't think that is a sufficient reason. You can make the docstrings wider or more concise if you want to scroll less, but if you have a lot of code docstrings let the IDE show you documentation, so you don't need to find the definition of the object, and other people using your code might not know what all the functions you are using (both imported and self-defined) do, and what the intended way to use them is. Also writing a docstring for a function/object is usually a one-time thing; when you copy the code to another project, you don't need to write a docstring anymore.
@WalterVos
@WalterVos Год назад
Heads up: the dry python domain has expired and was hijacked (quite hilarious advertising really). The project seems to use GitHub now as a homepage: github.com/dry-python
Далее
Cohesion and Coupling: Write BETTER PYTHON CODE Part 1
26:14
The purest coding style, where bugs are near impossible
10:25
A Simpler Way to See Results
19:17
Просмотров 98 тыс.
5 Good Python Habits
17:35
Просмотров 402 тыс.
The Strategy Pattern: Write BETTER PYTHON CODE Part 3
15:31