Why not just add "if __name__ == '__main__'" to the function in your function object?

This doesn't work because even if I use a function object in my file and write

def test():
   # my code here

test(test()  # will pass and return


the interpreter doesn't know which I'm calling which. I think it makes more sense to have the test() function be part of the global scope in a module and be declared at the top of the module, and call the function inside the module.

For example, I'm making a function to play a sound called say_play:

>>> def say_play():
   print "hello, world"
>>> play = say_play
>>> print play()
hello, world

It seems reasonable to me to have play() in my main module and also declare it in a function object with this name in my module object. And then when I call play(), the interpreter knows that I'm really calling play() and not something else (say_play).

So if you wanted to make a function that called some code in your module, would it make more sense to put all the code in a function object, and also in the global namespace with that name? That would allow you to call it anywhere and it would work