Must function defs appear earlier than calls?

Discussion in 'Python' started by SeeBelow@SeeBelow.Nut, May 4, 2004.

  1. Guest

    Must function defs appear earlier in a file than use of their name?

    If so, is there some way around this? It's creating a puzzle for me.

    Mitchell Timin

    --
    "Many are stubborn in pursuit of the path they have chosen, few in
    pursuit of the goal." - Friedrich Nietzsche

    http://annevolve.sourceforge.net is what I'm into nowadays.
    Humans may write to me at this address: zenguy at shaw dot ca
    , May 4, 2004
    #1
    1. Advertising

  2. John Roth Guest

    <> wrote in message news:...
    > Must function defs appear earlier in a file than use of their name?


    No. Function definitions must be executed before they
    are called, but they don't have to be before their
    calls.

    The reason for this is that it's the execution of the function
    definition at run time that binds the function to the name in
    the dictionary, and that's where it has to be to be executed.

    > If so, is there some way around this? It's creating a puzzle for me.


    Possibly you could say a bit more about what you're
    trying to do that's causing some perplexity?

    John Roth
    >
    > Mitchell Timin
    John Roth, May 4, 2004
    #2
    1. Advertising

  3. Guest

    John Roth wrote:
    >
    > <> wrote in message news:...
    > > Must function defs appear earlier in a file than use of their name?

    >
    > No. Function definitions must be executed before they
    > are called, but they don't have to be before their
    > calls.
    >
    > The reason for this is that it's the execution of the function
    > definition at run time that binds the function to the name in
    > the dictionary, and that's where it has to be to be executed.
    >
    > > If so, is there some way around this? It's creating a puzzle for me.

    >
    > Possibly you could say a bit more about what you're
    > trying to do that's causing some perplexity?
    >
    > John Roth
    > >
    > > Mitchell Timin


    I'm using Tkinter, creating buttons and scales. One of the buttons
    should have a command option to execute a function that initializes some
    class objects. These objects take a scale object as one of their
    initialization parameters, So I have to create the class objects after I
    create the scales.

    It seems that I must create the button that calls the initialization
    function after that, or else the function assigned to the command option
    is undefined.

    But I want to create the button first, so that it appears near the top
    of my frame.

    I hope that's clear. Now you see why I didn't describe that in my
    original post!

    And thanks for your help!

    Mitchell Timin

    --
    "Many are stubborn in pursuit of the path they have chosen, few in
    pursuit of the goal." - Friedrich Nietzsche

    http://annevolve.sourceforge.net is what I'm into nowadays.
    Humans may write to me at this address: zenguy at shaw dot ca
    , May 4, 2004
    #3
  4. At some point, wrote:

    > I'm using Tkinter, creating buttons and scales. One of the buttons
    > should have a command option to execute a function that initializes some
    > class objects. These objects take a scale object as one of their
    > initialization parameters, So I have to create the class objects after I
    > create the scales.
    >
    > It seems that I must create the button that calls the initialization
    > function after that, or else the function assigned to the command option
    > is undefined.
    >
    > But I want to create the button first, so that it appears near the top
    > of my frame.
    >
    > I hope that's clear. Now you see why I didn't describe that in my
    > original post!


    Some actual code would make what you're trying to do clearer. Here's
    my guess:

    def stuff(master):
    scale1 = Scale(master)
    scale1.pack()
    scale2 = Scale(master)
    scale2.pack()
    def init_classes(scales=[scale1, scale2]):
    ... stuff ...
    button = Button(master, command=init_classes)
    button.pack()

    and what you'd like is

    def stuff():
    # doesn't work as scale1 and scale2 haven't been assigned yet
    def init_classes(scales=[scale1, scale2]):
    ... stuff ...
    button = Button(master, command=init_classes)
    button.pack()
    scale1 = Scale(master)
    scale1.pack()
    scale2 = Scale(master)
    scale2.pack()

    You could use nested scopes like this:

    def stuff():
    def init_classes():
    scales = [scale1, scale2]
    ... stuff ...
    button = Button(master, command=init_classes)
    button.pack()
    scale1 = Scale(master)
    scale1.pack()
    scale2 = Scale(master)
    scale2.pack()

    Then, scale1 and scale2 aren't looked up until init_classes() is
    actually called, and the values are taken from the namespace of
    stuff(). By the time init_classes() is called, they should be assigned.

    Alternatively, you could move stuff() into a class, and assign scale1
    and scale2 as attributes of that class. init_classes should then be a
    method of the class.

    --
    |>|\/|<
    /--------------------------------------------------------------------------\
    |David M. Cooke
    |cookedm(at)physics(dot)mcmaster(dot)ca
    David M. Cooke, May 4, 2004
    #4
  5. In article <> (Tue, 04 May 2004 00:55:13 +0000),
    SeeBelow wrote:

    > Must function defs appear earlier in a file than use of their name?


    No. Function definitions must be executed before the name of the function
    is referenced. References can be calls but can also be arguments in
    method calls and values in assignment statements (and there are probably
    other cases but it's 2:00AM).
    Hamilcar Barca, May 4, 2004
    #5
  6. Eric Brunel Guest

    David M. Cooke wrote:
    [snip]
    > Some actual code would make what you're trying to do clearer. Here's
    > my guess:
    >
    > def stuff(master):
    > scale1 = Scale(master)
    > scale1.pack()
    > scale2 = Scale(master)
    > scale2.pack()
    > def init_classes(scales=[scale1, scale2]):
    > ... stuff ...
    > button = Button(master, command=init_classes)
    > button.pack()
    >
    > and what you'd like is
    >
    > def stuff():
    > # doesn't work as scale1 and scale2 haven't been assigned yet
    > def init_classes(scales=[scale1, scale2]):
    > ... stuff ...
    > button = Button(master, command=init_classes)
    > button.pack()
    > scale1 = Scale(master)
    > scale1.pack()
    > scale2 = Scale(master)
    > scale2.pack()


    Also note that you have no need to pack objects just after creating them. You
    can just do:

    def stuff(master):
    scale1 = Scale(master)
    scale2 = Scale(master)
    def init_classes(scales=[scale1, scale2]):
    ... stuff ...
    button = Button(master, command=init_classes)
    button.pack()
    scale1.pack()
    scale2.pack()

    Using the grid layout manager instead of the pack one may also be a solution to
    the problem, since with it, the layout does not depend on the order of the calls
    to grid as it does with pack.

    HTH
    --
    - Eric Brunel <eric (underscore) brunel (at) despammed (dot) com> -
    PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com
    Eric Brunel, May 4, 2004
    #6
  7. Guest

    Eric Brunel wrote:
    <snip>
    > Also note that you have no need to pack objects just after creating them. You
    > can just do:
    >
    > def stuff(master):
    > scale1 = Scale(master)
    > scale2 = Scale(master)
    > def init_classes(scales=[scale1, scale2]):
    > ... stuff ...
    > button = Button(master, command=init_classes)
    > button.pack()
    > scale1.pack()
    > scale2.pack()


    Thank You, that's what I was looking for. If I can define the scales
    early and pack them later, that solves my problem.

    m

    --
    "Many are stubborn in pursuit of the path they have chosen, few in
    pursuit of the goal." - Friedrich Nietzsche

    http://annevolve.sourceforge.net is what I'm into nowadays.
    Humans may write to me at this address: zenguy at shaw dot ca
    , May 4, 2004
    #7
  8. Daniel Yoo Guest

    wrote:
    : Must function defs appear earlier in a file than use of their name?

    : If so, is there some way around this? It's creating a puzzle for me.


    Let's work on a concrete example. Say we want to write something like
    this:

    ###
    print square(42)

    def square(x):
    return x * x
    ###

    This doesn't work, because when Python hits the 'print square(42)'
    statement, it's not aware yet of what 'square' means. But there is a
    way around this:


    ###
    def main():
    print square(42)

    def square(x):
    return x * x

    if __name__ == '__main__':
    main()
    ###


    Here, we enclose the main flow of our program in a function called
    main(). Python doesn't evaluate a function's body until it is called,
    so by the time that we hit:

    if __name__ == '__main__':
    main()

    we're ok, since both main() and square() are defined.


    So to answer your question:

    : Must function defs appear earlier in a file than use of their name?

    we can allow the use to be earlier in terms of location in the source
    file, by using a function definition to delay the evaluation till all
    the symbols are in place.


    Hope this helps!
    Daniel Yoo, May 5, 2004
    #8
  9. Guest

    Daniel Yoo wrote:
    >
    > wrote:
    > : Must function defs appear earlier in a file than use of their name?
    >
    > : If so, is there some way around this? It's creating a puzzle for me.
    >
    > Let's work on a concrete example. Say we want to write something like
    > this:
    >
    > ###
    > print square(42)
    >
    > def square(x):
    > return x * x
    > ###
    >
    > This doesn't work, because when Python hits the 'print square(42)'
    > statement, it's not aware yet of what 'square' means. But there is a
    > way around this:
    >
    > ###
    > def main():
    > print square(42)
    >
    > def square(x):
    > return x * x
    >
    > if __name__ == '__main__':
    > main()
    > ###
    >
    > Here, we enclose the main flow of our program in a function called
    > main(). Python doesn't evaluate a function's body until it is called,
    > so by the time that we hit:
    >
    > if __name__ == '__main__':
    > main()
    >
    > we're ok, since both main() and square() are defined.
    >
    > So to answer your question:
    >
    > : Must function defs appear earlier in a file than use of their name?
    >
    > we can allow the use to be earlier in terms of location in the source
    > file, by using a function definition to delay the evaluation till all
    > the symbols are in place.
    >
    > Hope this helps!


    Yes, this is good info!

    thanks,

    Mitchell Timin

    --
    "Many are stubborn in pursuit of the path they have chosen, few in
    pursuit of the goal." - Friedrich Nietzsche

    http://annevolve.sourceforge.net is what I'm into nowadays.
    Humans may write to me at this address: zenguy at shaw dot ca
    , May 5, 2004
    #9
    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. Gelmir Tinehtelë
    Replies:
    10
    Views:
    667
    Aidan
    Jun 9, 2004
  2. Joey Martin
    Replies:
    28
    Views:
    244
    Bob Barrows [MVP]
    Jun 25, 2008
  3. Kevin Howe

    dates earlier than 1970?

    Kevin Howe, Nov 3, 2004, in forum: Ruby
    Replies:
    2
    Views:
    115
    Yukihiro Matsumoto
    Nov 3, 2004
  4. Replies:
    1
    Views:
    191
    Jano Svitok
    Jul 17, 2007
  5. Bob
    Replies:
    5
    Views:
    240
Loading...

Share This Page