In other languages I often do something like this:
someFunc()
someFunc() {
// This is my function
}
This way I can stack all my functions lower in the file, but make the function calls at the top. Now I have a nice overview of everything that's happening.
However, when I did this in Python 3 in Spyder, I got the warning that Undefined name: 'myfunc'
my_func("Some string")
def my_func(some_var):
print(some_var)
The code works fine, but I'm not sure what best practice here. Are there any negative impacts caused by my method? Or is it merely a guideline to have your functions before you call them?
The code I set above does work for me. Why is that? I'm running Python 3.4.3 with Anaconda. What's different about my version? Or is it because I run it in Spyder?
Edit: so apparently Spyder works in mysterious ways. First I had the call after the definition, which worked, then I swapped the call to the first line and it still worked. Spyder seems to cache functions or at least not flushing them out. (Though I'm not sure if it's Spyder that's doing the caching or Python itself. I'm assuming Python.) For any newbies out there wondering about this: solution is to restart your programme and/or Python service.