In Python it's possible to create a procedure that has no explicit return. i.e.:
def test(val):
if 0 == val:
return 8
Further, it's possible to assign the results of that function to a variable:
>>> a = test(7)
>>> print `a`
'None'
Why the heck is that? What language logic is behind that baffling design decision? Why doesn't that simply raise a compiler error?
EDIT: Yes, I realise that it works that way. Thanks. My question is WHY? It seems like such a sure fire way to introduce subtle bugs into your code. And it seems, as was mentioned below by e-satis, to go against the very wise pythonic adage that "explicit is better then implicit".
So, what's up with this? Is it just a design oversight or a simplifying assumption or a deliberate, considered decision?
EDIT: Would everyone agree that expressing the intent this way is much better:
def test(val):
if 0 == val:
return 8
else:
return None
If so, why would Python prefer the former style to raising a compile time exception?
EDIT: S.Lott brings up the point explicitly (and others do too, his just seems most clear to me) that this behaviour is a result of the fact that Python is a dynamic language and therefor can't check at compile time, the run time behaviour of the system.
Since there is no distinction between procedures (those functions that return no values) and functions (those that do), that there is nothing to enforce at compile time. Which means that when we come to run-time behaviour, we either have a catastrophic failure in some cases (we throw an exception) or we silently fail assuming that the programmer knew what they were doing and understood the default behaviour.
The pythonic way is to do the latter, the C-style way is to do the former.
That seems to make sense as a good reason to do it that way.
S.Lott's clear justification is buried in the comments to the accepted answer so I thought it best to summarise here.
I'll hold off on accepting the answer for a bit to see if S.Lott makes an official answer. Otherwise, I'll give the points to SilentGhost.
Thanks all.