Where do Python functions return their values?

For a python function without a return statement, the function result is returned to the
function object that called it ???

Whats the difference between that and the return statement ?

All Python functions have a return. If you don’t specify a return explicitly , it’ll return None.

Understood, but what about the python example below, curious for your thoughts:

def simpleMath(a,b,c):
sum = a + b + c
print(sum) # 6 is returned to the variable sum outside of the function, doesn’t that go
# against the scope rules, or is that considered the return ?

Are you sure your code doesn’t have some dirty variables you set earlier?

Run it in a clean session. You should either see that sum is a built in function when you print , or if you change the variable name, a name error because your variable doesn’t exist.

The only way something would leak out is

A) you have a faulty Python interpreter (highly unlikely)
B) you used the global keyword and then set it inside your function (generally bad code)
C) your actual code is passing a mutable object in like a list and you’re observing a mutation of an object. (But your code snippet here is non mutating)

You can see that Python doesn’t escape the scope here


I suspect your real example is running in a dirty session or something else is afoot.

Thanks for the info, very cool online python compiler.
You were totally right… dirty variable. I was wondering what was going on.