Calling functions before that are def'ed

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Charles Larry

    Calling functions before that are def'ed

    Is there a way to define functions after the main part of a Python
    script?

    Example:

    #!/usr/local/bin/python

    # this code yields a NameError

    print_message(" hello world")

    def print_message(m sg):
    print msg


    I know functions can be put in separate files and imported, but I want
    everything in a single file. Am I stuck with defining all the
    functions first? If so, how come at compile-time Python can't look
    ahead in the program and discover the existence of functions referred
    to but not yet defined?

    Regards,
    Charles
  • Michael Geary

    #2
    Re: Calling functions before that are def'ed

    Charles Larry wrote:[color=blue]
    > Is there a way to define functions after the main part of a Python
    > script?
    >
    > Example:
    >
    > #!/usr/local/bin/python
    >
    > # this code yields a NameError
    >
    > print_message(" hello world")
    >
    > def print_message(m sg):
    > print msg
    >
    > I know functions can be put in separate files and imported, but I want
    > everything in a single file. Am I stuck with defining all the
    > functions first? If so, how come at compile-time Python can't look
    > ahead in the program and discover the existence of functions referred
    > to but not yet defined?[/color]

    Function definitions are executable statements, so like any other statement
    they are executed in the order they are encountered.

    If you prefer the style of having functions after your main program, simply
    define a main function at the top of your script, and call it at the end.

    -Mike


    Comment

    • Daniel Dittmar

      #3
      Re: Calling functions before that are def'ed

      Charles Larry wrote:[color=blue]
      > Is there a way to define functions after the main part of a Python
      > script?
      >
      > Example:
      >
      > #!/usr/local/bin/python
      >
      > # this code yields a NameError
      >
      > print_message(" hello world")
      >
      > def print_message(m sg):
      > print msg[/color]

      #!/usr/local/bin/python

      def main ():
      print_message(" hello world")

      def print_message(m sg):
      print msg

      if __name__ == "__main__":
      main ()

      def statements are really executable statements and not declarations, so
      the order is important.

      Daniel

      Comment

      • Peter Hansen

        #4
        Re: Calling functions before that are def'ed

        Daniel Dittmar wrote:[color=blue]
        >
        > Charles Larry wrote:[color=green]
        > > Is there a way to define functions after the main part of a Python
        > > script?
        > >
        > > Example:
        > >
        > > #!/usr/local/bin/python
        > >
        > > # this code yields a NameError
        > >
        > > print_message(" hello world")
        > >
        > > def print_message(m sg):
        > > print msg[/color]
        >
        > #!/usr/local/bin/python
        >
        > def main ():
        > print_message(" hello world")
        >
        > def print_message(m sg):
        > print msg
        >
        > if __name__ == "__main__":
        > main ()
        >
        > def statements are really executable statements and not declarations, so
        > the order is important.[/color]

        Note that the above slightly changes the behaviour of the code when globals
        are involved. Putting the original code directly under the __main__ part
        at the end, however, does not. (This example is too simple to exhibit any
        difference in behaviour, but the following is the "safer" method for the
        unwary newbie, even if it's not as clean in the long run.)

        def print_message(m sg):
        print msg

        if __name__ == "__main__":
        print_message(" hello world")

        -Peter

        Comment

        Working...