-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
cclloyd
  • 8,171
  • 16
  • 57
  • 104
  • Why would you want that? – Tim Mar 09 '15 at 23:43
  • not really .... not sure why you would really want this though ... – Joran Beasley Mar 09 '15 at 23:43
  • possible duplicate of [Does Python have class prototypes (or forward declarations)?](http://stackoverflow.com/questions/524714/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](http://stackoverflow.com/questions/28514022/implementing-forward-declarations-for-functions-in-python). – ljetibo Mar 09 '15 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. – Hugh Bothwell Mar 09 '15 at 23:51

1 Answers1

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.

user2357112
  • 260,549
  • 28
  • 431
  • 505