Function definition

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

    #1

    Function definition

    I have a little .py file that has the format:


    def fxn(a):
    do stuff

    other stuff
    ....
    r = fxn(a)
    .....


    Now I've tried putting the function declaration after the call but the
    program wouldn't work. Is there anyway to put function declarations at
    the end of the program, rather than putting them at the beginning,
    which is rather clunky?

    Thanks.
    Aaron

  • faulkner

    #2
    Re: Function definition

    no.
    python is not C. python is interpreted, not compiled, so if you want a
    function to exist when you call it, you need to define it before you
    call it.
    it isn't clunky, it's just how it's done.
    if you want to define a 'main' function at the top of your
    script/module, go for it. then you can use this:
    if __name__ == '__main__': sys.exit(main(* sys.argv[1:]))

    aarondesk wrote:[color=blue]
    > I have a little .py file that has the format:
    >
    >
    > def fxn(a):
    > do stuff
    >
    > other stuff
    > ...
    > r = fxn(a)
    > ....
    >
    >
    > Now I've tried putting the function declaration after the call but the
    > program wouldn't work. Is there anyway to put function declarations at
    > the end of the program, rather than putting them at the beginning,
    > which is rather clunky?
    >
    > Thanks.
    > Aaron[/color]

    Comment

    • forman.simon@gmail.com

      #3
      Re: Function definition

      aarondesk wrote:
      ....[color=blue]
      >
      > Now I've tried putting the function declaration after the call but the
      > program wouldn't work. Is there anyway to put function declarations at
      > the end of the program, rather than putting them at the beginning,
      > which is rather clunky?
      >
      > Thanks.
      > Aaron[/color]

      A function can call functions defined "below" it:

      def a():
      return b()

      def b():
      return 'foo'

      print a() # prints 'foo'

      because at runtime both functions exist in the current namespace. But
      module-level statements can't call functions defined below them because
      the function objects haven't been created yet. That's why you get a
      NameError, the name 'b' won't been bound to anything until after the
      def statement has been interpreted.

      You can, of course, put your functions in another module.

      Hope that helps,
      ~Simon

      Comment

      • Duncan Booth

        #4
        Re: Function definition

        aarondesk wrote:
        [color=blue]
        > I have a little .py file that has the format:
        >
        >
        > def fxn(a):
        > do stuff
        >
        > other stuff
        > ...
        > r = fxn(a)
        > ....
        >
        >
        > Now I've tried putting the function declaration after the call but the
        > program wouldn't work. Is there anyway to put function declarations at
        > the end of the program, rather than putting them at the beginning,
        > which is rather clunky?
        >[/color]

        How about putting your main program code inside another function called
        something like 'main'? Then it can appear wherever you like. All you need
        at the end of the file then is:

        if __name__=='__ma in__':
        main()

        This also means you can reuse the functions with different main code simply
        by importing them from another file.

        Comment

        • bruno at modulix

          #5
          Re: Function definition

          faulkner wrote:
          (pelase don't top-post - fixed)[color=blue]
          > aarondesk wrote:
          >[/color]
          (snip)[color=blue][color=green]
          >>Now I've tried putting the function declaration after the call but the
          >>program wouldn't work. Is there anyway to put function declarations at
          >>the end of the program, rather than putting them at the beginning,
          >>which is rather clunky?[/color][/color]
          [color=blue]
          > no.
          > python is not C. python is interpreted, not compiled,[/color]

          Please verify your assertions... FWIW, being "compiled" or "interprete d"
          is not a feature of a language, but of an implementation of a language.
          And FWIW also, CPython *is* compiled (to byte-code, like Java).



          --
          bruno desthuilliers
          python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
          p in 'onurb@xiludom. gro'.split('@')])"

          Comment

          Working...