Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Return error from function

Tags:

I am writing a python function which uses two arrays of equal size [n,1]. Before performing any calculations, I'd like to check to ensure the lengths are the same, and if not, return an error. What is the best practice?

def do_some_stuff(array1, array2):  # Before doing stuff, check to ensure both arrays have the same length  if len(array1) != len(array2): #  Break and return with error 

I'm confused, because I want to break and return an error code (say -1). Seems that break will return without any value, and return will continue to execute the function? Or does Return break out of any remaining code?

like image 504
GPB Avatar asked Nov 27 '15 17:11

GPB


People also ask

How do you return an exception raised in Python?

Define a new exception as subclass of Exception, and raise that. raise MyError(x['error']) , after determining that the value exists.

Can you throw an error in Python?

As a Python developer you can choose to throw an exception if a condition occurs. To throw (or raise) an exception, use the raise keyword.

How do you catch errors in Python?

The try block lets you test a block of code for errors. The except block lets you handle the error. The else block lets you execute code when there is no error. The finally block lets you execute code, regardless of the result of the try- and except blocks.

What is the use of return in Python?

The Python return statement is a key component of functions and methods. You can use the return statement to make your functions send Python objects back to the caller code. These objects are known as the function’s return value. You can use them to perform further computation in your programs.

How do I return none in Python?

Note: The Python interpreter doesn’t display None. So, to show a return value of None in an interactive session, you need to explicitly use print (). Regardless of how long and complex your functions are, any function without an explicit return statement, or one with a return statement without a return value, will return None.

What is the default return value of a function in Python?

A Python function will always have a return value. There is no notion of procedure or routine in Python. So, if you don’t explicitly use a return value in a return statement, or if you totally omit the return statement, then Python will implicitly return a default value for you. That default return value will always be None.

Why do we return a function when we factor in Python?

The function object you return is a closure that retains information about the state of factor. In other words, it remembers the value of factor between calls. That’s why double remembers that factor was equal to 2 and triple remembers that factor was equal to 3.


2 Answers

What is the best practice?

In python code, error conditions are usually indicated with exceptions. You could use raise ValueError("Arrays must have the same size").

Using exception rather than return values to indicate errors has the added advantage that the exception would bubble up until it reaches a except statement. So you can defer the error handling up to a place where it makes sense. Also exceptions have an associated descriptive message instead of a magic number.

The break statement, as in many other languages, is used to interrupt the flow in loops, like ones created with while or for.

like image 185
memoselyk Avatar answered Oct 11 '22 11:10

memoselyk


You don't need the break

def do_some_stuff(array1, array2):      # Before doing stuff, check to ensure both arrays have the same length     if len(array1) != len(array2):        return -1 

Just return the error code. In this way the rest of the code of the function will not be executed.

like image 20
k4ppa Avatar answered Oct 11 '22 12:10

k4ppa