Must function defs appear earlier than calls?

Eric Brunel eric_brunel at despammed.com
Tue May 4 05:00:38 EDT 2004


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




More information about the Python-list mailing list