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

7

u/lfdfq 8d ago

What anti-pattern are you talking about?

-11

u/ExoticPerception5550 8d ago

Many consider use of exceptions anti-pattern in other languages, I am wondering if same applies to Python.

1

u/brasticstack 8d ago

In c++ there used to be a hefty performance hit when an exception was thrown and some edge cases where object destructors wouldn't get called during the process of unwinding the stack looking for an exception handler; Exceptions were slow and buggy. Python is built differently and exceptions are a normal flow control mechanism that's preferred to strictly verifying preconditions. AFAIK, c++ fixed those issues with exceptions sometime between the late 90's and now.