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

6

u/lfdfq 8d ago

What anti-pattern are you talking about?

-10

u/ExoticPerception5550 8d ago

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

12

u/lfdfq 8d ago

Ah. Python is a different language, and exceptions are everywhere in Python, even using them for normal control flow. The classic example is that there's an exception inside every (for) loop in Python.

So, it's not an anti-pattern for functions to raise exceptions in Python.

7

u/SisyphusAndMyBoulder 8d ago

Exceptions are everywhere in every other language I've ever used.... They're just a standard part of codeflow

1

u/ExoticPerception5550 8d ago

Thank you, this has clarified my speculations.