Must function defs appear earlier than calls?

S

SeeBelow

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
 
J

John Roth

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
 
S

SeeBelow

John said:
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.


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

John Roth

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
 
D

David M. Cooke

At said:
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.
 
H

Hamilcar Barca

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).
 
E

Eric Brunel

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
 
S

SeeBelow

Eric Brunel wrote:
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
 
D

Daniel Yoo

(e-mail address removed) 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!
 
S

SeeBelow

Daniel said:
(e-mail address removed) 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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,121
Latest member
LowellMcGu
Top