[ACCEPTED]-Python procedure return values-python

Accepted answer
Score: 28

While every function might not have an explicit 11 return it will have an implicit one, that 10 is None, which incidentally is a normal Python 9 object. Further, functions often return None explicitly.

I 8 suppose returning None implicitly is just 7 an ease-of-use optimisation.

P.S. I wonder what 6 you propose compile would do in the following 5 case:

def t():
    if True:
        return 'ham'

P.P.S. return statement indicates that something 4 needs to be returned, be it None, 42 or 'spam'. Some functions, however, don't 3 really return anything, like list.sort or __init__, therefore 2 it would be an overhead to require a return None statement 1 at the end of each __init__.

Score: 12

Python never checks for human misbehavior. The 9 language philosophy is to assume that programmers 8 are grown up adults and know what they are 7 doing.

Since :

  • Python is an autodocumented language, so you'll know in a blink what returns a function;
  • Python uses duck typing, so getting None by default can ease dynamic function call a lot.

Returning None make more senss 6 than raising an error.

E.G :

function_stack 5 = [func1, func2, func3]

for f in function_stack :
    a = f()
    if a is not None :
        # do something

No matter if f is 4 f() is a function or a procedure. There 3 is no such thing in Python. You can apply 2 all the function, and process output if 1 there is any.

Score: 7

Heh. Coming at it from a Java perspective, your 8 question is obvious. It should throw an 7 error.

But if you came at it from a Perl 6 perspective, it's not obvious at all. You're 5 setting a variable to no value, big deal.

It's 4 all about the typing. Like Java, Python 3 is strongly typed, but unlike Java, it is also dynamically typed. Most 2 dynamically typed languages allow shenanigans 1 like this.

Score: 3

It's beautiful ...

It returns None in the case where val != 0 which to 6 me make sense.

Python 2.6.1 (r261:67515, Jun 18 2009, 17:24:16)
[GCC 3.3.3 (SuSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def test(val):
...     if 0 == val:
...         return 8
>>> a = test(8)
>>> print a
>>> print test(0)

Do you have any arguments 5 why you think the behavior is wrong?

You 4 can use c++ if you need strong compile time 3 type checking but for python it seems extremely 2 flexible and I assume that's what you want 1 when you selected python for your task.

Score: 2

What compiler? The bytecode compiler checks 10 your code for syntax on import, but syntax 9 doesn't include "check all paths for a return". So 8 when test() gets called at runtime, and 7 the value of x isn't 0 and we just fall 6 out of the function, what should we do? return 5 0? Why not -1, or "", or -infinity? So 4 a nice neutral value should always be returned 3 in the absence of an explicit return statement 2 - and that value is None. This is a standard 1 feature of the language - welcome to Python!

Score: 1

It's because of the dynamic typing.

You don't have to make 5 the distinction between, say, turbo pascal's 4 procedure and function. They are all function, they 3 return None by default, which is logically correct. If 2 you don't say anything it returns nothing, None.

I 1 hope it make more sense now-

More Related questions