The pythonic thing to do is to raise and handle exceptions. The excellent book “Python in a nutshell” discusses this in ‘Error-Checking Strategies’ in Chapter 6.
The book discusses EAFP (“it’s easier to ask forgiveness than permission”) vs. LBYL (“look before you leap”).
So to answer your question:
No, I would not recommend the same for python code. I suggest you read chapter 6 of Python in a nutshell.
Related Contents:
- Cost of exception handlers in Python
- Manually raising (throwing) an exception in Python
- String formatting: % vs. .format vs. f-string literal
- How to catch and print the full exception traceback without halting/exiting the program?
- Fastest way to check if a value exists in a list
- How do you test that a Python function throws an exception?
- Speed comparison with Project Euler: C vs Python vs Erlang vs Haskell
- Is it a good practice to use try-except-else in Python?
- Python: How to ignore an exception and proceed? [duplicate]
- Why is “except: pass” a bad programming practice?
- Why doesn’t list have safe “get” method like dictionary?
- Frequency counts for unique values in a NumPy array
- Why are some float < integer comparisons four times slower than others?
- Fastest way to get the first object from a queryset in django?
- Creating an empty list in Python
- Which is faster in Python: x**.5 or math.sqrt(x)?
- How to get the name of an exception that was caught in Python?
- What is the most efficient way of finding all the factors of a number in Python?
- [] and {} vs list() and dict(), which is better? [closed]
- Does pandas iterrows have performance issues?
- What is a good way to handle exceptions when trying to read a file in python?
- Why is any (True for … if cond) much faster than any (cond for …)?
- In Python, what’s the difference between ‘except Exception as e’ and ‘except Exception, e’ [duplicate]
- Fast way to copy dictionary in Python
- Python: get a dict from a list based on something inside the dict
- Python type hinting with exceptions
- How can I safely create a directory (possibly including intermediate directories)?
- Python’s sum vs. NumPy’s numpy.sum
- How can I get a Python generator to return None rather than StopIteration?
- Text processing – Python vs Perl performance [closed]
- Best Practices for Python Exceptions? [duplicate]
- Why is a.insert(0,0) much slower than a[0:0]=[0]?
- Python threads all executing on a single core
- How do I log an exception at warning- or info-level with traceback using the python logging framework?
- How to get the last exception object after an error is raised at a Python prompt?
- Printing out actual error message for ValueError
- List of all unique characters in a string?
- Complexity of list.index(x) in Python
- How can I make a for-loop pyramid more concise in Python? [duplicate]
- Case-insensitive string startswith in Python
- Python, mock: raise exception [closed]
- Why is Numpy with Ryzen Threadripper so much slower than Xeon?
- python try:except:finally
- How to know time spent on each test when using unittest?
- Python readlines() usage and efficient practice for reading
- How “with” is better than try/catch to open a file in Python?
- Get key name from Python KeyError exception
- Difference between IOError and OSError?
- raise with no argument
- Why is this loop faster than a dictionary comprehension for creating a dictionary?