-1

Is there a way (without putting the function in a separate file) to define the content of a function after the bulk of my code? Sort of like C where you define prototypes and put the body later on in the file.

Ex;

blah blah blah
functionCall(arg)
blah blah blah

def functionCall(arg):
   blah blah blah
4
  • Why would you want that? Commented Mar 9, 2015 at 23:43
  • not really .... not sure why you would really want this though ... Commented Mar 9, 2015 at 23:43
  • possible duplicate of Does Python have class prototypes (or forward declarations)?. It seems I've also might've linked the wrong thing, but this question has almost certainly been asked before. Commented Mar 9, 2015 at 23:43
  • It doesn't matter where in the file your function is defined, so long as it is defined before it is called. Commented Mar 9, 2015 at 23:51

1 Answer 1

3

Yes. Instead of

blah blah blah
functionCall(arg)
blah blah blah

def functionCall(arg):
    blah blah blah

Do

def main():
    blah blah blah
    functionCall(arg)
    blah blah blah

def functionCall(arg):
    blah blah blah

if __name__ == '__main__':
    main()

The if __name__ == '__main__': bit is mostly unrelated to the topic of the question; it just prevents the main from running if this file is imported as a module.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.