python function defs/declarations

Discussion in 'Python' started by bruce, Jul 3, 2006.

  1. bruce

    bruce Guest

    hi..

    the docs state that the following is valid...

    def foo():
    i = 2
    print "i = "i

    print "hello"
    foo()


    is there a way for me to do this..

    print "hello"
    foo()

    def foo():
    i = 2
    print "i = "i

    ie, to use 'foo' prior to the declaration of 'foo'

    thanks

    -bruce
     
    bruce, Jul 3, 2006
    #1
    1. Advertising

  2. bruce <> wrote:

    > hi..
    >
    > the docs state that the following is valid...
    >
    > def foo():
    > i = 2
    > print "i = "i
    >
    > print "hello"
    > foo()
    >
    >
    > is there a way for me to do this..
    >
    > print "hello"
    > foo()
    >
    > def foo():
    > i = 2
    > print "i = "i
    >
    > ie, to use 'foo' prior to the declaration of 'foo'


    There are no declarations in Python. "def" is an executable statement:
    when executes it binds a new function object to the given name.

    So, your request is like asking to do, say:

    print "hello"
    print wap

    wap = "world"

    At the time you use name wap, nothing is bound to it; the fact that
    something would later be bound to it (if the binding statement, here an
    assignment but that's exactly as much of an executable statement as a
    def!) is pretty clearly irrelevant. Having a clear idea about these
    issues is why it's important to remember the distinction between
    executable statements (including def, class, assignments, ...) and
    declarations (which Python does not have).

    You can probably wrap your code in a function, and call it at the end:

    def main():
    print "hello"
    foo()

    def foo(): ...whatever...

    main()


    By the time the body of main executes, "def foo" has already executed,
    so global name foo is happily bound and everything works fine.

    Wrapping most substantial code inside functions is VERY advisable anyway
    -- just put just about all the code you'd like to have at module top
    level (except for def, class and assignments to "module constants") into
    a function (conventionally named main) and call that function at the
    very end of the module (ideally within an "if __name__=='__main__":"
    guard, but that's a different issue!).


    Alex
     
    Alex Martelli, Jul 3, 2006
    #2
    1. Advertising

  3. bruce

    John Salerno Guest

    bruce wrote:

    > the docs state that the following is valid...


    > print "i = "i


    Is this a typo or is that really in the docs?
     
    John Salerno, Jul 5, 2006
    #3
  4. bruce

    Guest

    bruce:
    > is there a way for me to do this..
    >
    > print "hello"
    > foo()
    >
    > def foo():
    > i = 2
    > print "i = "i
    >
    > ie, to use 'foo' prior to the declaration of 'foo'


    Generally no you can't, you have to define a name before using it.
    Why do you want to do that?

    Bye,
    bearophile
     
    , Jul 5, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nooobody
    Replies:
    0
    Views:
    524
    nooobody
    Nov 30, 2004
  2. Replies:
    8
    Views:
    297
  3. Stephen Mayes

    Standard return defs?

    Stephen Mayes, Aug 11, 2004, in forum: C Programming
    Replies:
    4
    Views:
    332
    Joona I Palaste
    Aug 12, 2004
  4. G Patel

    .defs & .s file extension

    G Patel, Mar 7, 2005, in forum: C Programming
    Replies:
    2
    Views:
    526
    James McIninch
    Mar 8, 2005
  5. TC
    Replies:
    5
    Views:
    262
Loading...

Share This Page