0

I'm wondering if I can call a function in python above its actual definition. I want to put all of my functions at the end of the source code, but I am not sure if this will work. Thanks!

2 Answers2

1

The convention when using python is to call the main function at the end this will resolve any issue you might have with function calls, as the comments suggest, try it out! if that doesn't work, look it up :)

The link @Andy posted does answer your questions (link for reference)

Just make sure to add this:

if __name__ == '__main__':
    main()

at the end of your code.

Community
  • 1
  • 1
MrHaze
  • 3,786
  • 3
  • 26
  • 47
-1

The answer is yes, you can call a function before its defined in the same file.

PsyKzz
  • 740
  • 4
  • 14
  • This is not reliably true: It depends on how the file is being run, and what context the call is in/from. You can in some cases do this with a call _from another function_, f/e, but not one from the top level. See [this answer](https://stackoverflow.com/a/64110553/14122) on the linked duplicate for more details. – Charles Duffy Jul 03 '22 at 03:00