is it possible to not return anything from a function in python?
with a simple filter that test input against a range 0-100.
def foo(foo_input): if 0 <= foo_input <= 100: return f_input
This returns none if
foo_input is > 100. But could it actually 'not' return anything? or does a function allways have to return something?
is it possible to not return anything from a function in python?, There is no such thing as "returning nothing" in Python. Every function returns some value (unless it raises an exception). If no explicit return Functions always return something (at least None, when no return-statement was reached during execution and the end of the function is reached). Another case is when they are interrupted by exceptions. In this case exception handling will "dominate over the stack" and you will return to the appropriate except or get some nasty error :)
No. If a
return statement is not reached before the end of the function then an implicit
None is returned.
Is there a way to return literally nothing in python?, Third, The '0' you are seeing is not a return value from the Insert function, but an output from Is it normal to write functions instead of classes in python? Long answer: The return statement is not required in Python, and reaching the end of the function's code without a return will make the function automatically return None. Additional question: What does a function return? It returns the objects provided to the return statement if found one. If no return statement is found, None is returned.
If a return statement is not reached, the function returns
def set_x(): x = 2
What should I do if my Python function isn't returning anything?, There are two kinds of functions: fruitful, or value-returning functions, which end of any function and we have not explicitly executed any return statement, Python important operations on strings, and we'll be able to do just about anything. Python Function Return Multiple Values. There is no return and the calling code does not attempt to use (e. Then by calling the function, we calculate the Sum and Average of that three numbers. Because the return statement can contain any Python expression we could have avoided creating the temporary variable y and simply used return x*x.
I'm not sure what you really are trying to do. Here are a few things you might like:
def foo(foo_input, foo_default): if 0 <= foo_input <= 100: return f_input else: return foo_default def foo(foo_input): if 0 <= foo_input <= 100: return f_input raise ValueError, "foo_input was not in range [0, 100]"
Wait, you said "filter". Are you filtering a series of values and you just want to extract the ones that meet a criteria? That's easy in Python:
def foo_check(x): return 0 <= x <= 100 filtered_list = [x for x in unfiltered_sequence if foo_check(x)]
And you said "chaining functions". Again that's easy if we are talking about filtering a sequence:
def foo_filter(seq): for x in seq: if 0 <= x <= 100: yield x def other_filter(seq): for x in seq: if meets_criterion(x): yield x def do_the_task(seq): for x in other_filter(foo_filter(seq)): do_something(x)
EDIT: Here is a nice introduction to iterators and generators in Python. http://www.learningpython.com/2009/02/23/iterators-iterables-and-generators-oh-my/
Learning with Python 3, Python has a built-in function for computing the absolute value: In many other languages, a chunk that doesn't return a value is called a procedure, but we will Notice something important here. 3, print(y) # Bad! should use return instead! Functions can display data directly in Python or they can return the data to the caller so that the caller can do something more with it. In some cases, a function displays data directly as well as returns data to the caller, but it’s more common for a function to either display the data directly or to return it to the caller.
I sort of like the implicit
return None but pylint flags it as bad style, warning:
Either all return statements in a function should return an expression, or none of them should.pylint(inconsistent-return-statements)
def foo(foo_input): if 0 <= foo_input <= 100: return f_input return None
might be better style, even if they are functionally the same.
More info available here, where the Pylint change-log states:
A new Python checker was added to warn about inconsistent-return-statements. A function or a method has inconsistent return statements if it returns both explicit and implicit values ...
According to PEP8, if any return statement returns an expression, any return statements where no value is returned should explicitly state this as return None, and an explicit return statement should be present at the end of the function (if reachable).
6.2. Functions that Return Values, In Python, every function returns something. If there are no return statements, then it returns None. If the return Select which are true for Python functionGroup of answer choicesPython function doesn’t return anythingA Python function can only return a single valueA Python function can return multiple valuesA function only executes when it is called and we can call it multiple times in a program
Python return Statement, Introduction to functions in Python with and without parameters. to reuse code by copying it and adapting it to different contexts, which would be a bad idea. If there is no return statement in the function code, the function ends, when the Therefore anything we do to s inside of the function body of f is done to the global It is even possible to have a single function return multiple values with only a single return: See the Python Docs for more info. If this article was helpful
Python Tutorial: Functions, Note: Return statement can not be used outside the function. Syntax: function. This is possible because funcitons are treated as first class objects in Python. It is not necessary for a function to accept parameters and return values. It can either do both, or one of them, or none. Below is an example of a function which does not take any parameter but returns data. from random import seed, random from random import random def generate_random_number (): seed (10) return random ()
Python return statement, append() does not actually return a list, it merely updates the list object in place. (I.e. there is no return value from the function. Other programming A function in Python is defined with the def keyword. Functions do not have declared return types. A function without an explicit return statement returns None. In the case of no arguments and no return value, the definition is very simple.