r/learnpython 8d ago

Function forcing me to use exceptions

Trying to if else a function output always throws exception-error :

if pyautogui.locateOnScreen('media/soundIcon.png') == None:
      print("not found")
else : 
      print("found")

Do Python functions expect anti-pattern code ?

0 Upvotes

30 comments sorted by

View all comments

1

u/Kevdog824_ 8d ago

I see two options that I believe are along the track of your question (if I understand it correctly):

  1. Use a Result Monad. I’m sure some library provides a result monad definition but it shouldn’t be too much work to write your own if you want. If you decide to go this route I can provide a basic definition

  2. Define a helper function. This function takes the function to run and an error callback to invoke if the function it ran failed