is it possible to not return anything from a function in python?

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 :)

Regarding your problem I must say there are two possibilities: Either you have something to return or you do not have.

  • If you have something to return then do so, if not then don't.
  • If you rely on something being returned that has a certain type but you cannot return anything meaningful of this type then None will tell the caller that this was the case ( There is no better way to tell the caller that "nothing" is returned then by None, so check for it and you will be fine)

If a return statement is not reached, the function returns None.

def set_x():
    x = 2

No. If a return statement is not reached before the end of the function then an implicit None is returned.


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/