Mindboggling Scope Issue

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

    #1

    Mindboggling Scope Issue

    Hello All,

    I originally posted this to the help list, but I wanted to try a broader
    audience. Its so bizarre, that it may take some discourse to understand what
    is going on.

    Far below are two alternative methods I have made inside a class. In "method1"
    I create a name called "passWindow.res ult" and use it in the "ok()" function.
    In "method2", I simply call this name "result".

    Matt Cowles gave this explanation:[color=blue]
    > The important difference is that you assign to the variable result
    > later in the function. If a variable isn't assigned to in a function,
    > Python looks for it in enclosing namespaces:[/color]

    If I understand Matt's answer, "ask_for_passwo rd(self,message )", should be the
    enclosing namespace for "ok()". So I am still confused: notice how I defined
    "passWindow " on the line preceding the "result=Non e" (method2) or
    "passWindow.res ult=None" (method1) statments. I haven't defined "passWindow "
    before I defined it in the "get_password(s elf)" method, i.e. "passWindow ",
    like "result" and "passWindow.res ult", is not part of the global name space
    or any other enclosing namespace beyond the "get_password(s elf)" method.

    Testing Matt's explanation, I began a fresh CLI session (using the same python
    executable used for my program). The session is cut and pasted from my
    terminal:

    % python
    Python 2.3.3 (#2, Feb 17 2004, 11:45:40)
    [GCC 3.3.2 (Mandrake Linux 10.0 3.3.2-6mdk)] on linux2
    Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
    >>> def outer():[/color][/color][/color]
    .... def inner():
    .... print bob
    .... bob = "wuzzup?"
    .... inner()
    ....[color=blue][color=green][color=darkred]
    >>> outer()[/color][/color][/color]
    wuzzup?

    This works as I expect and in accord with Matt's explanation and the Laws of
    Common Sense, but it appears to be entirely inconsistent with the behavior of
    "method2" below. Thus, I am completely bewildered.

    I have pasted the methods below (sorry they are so long, but I didn't want
    to alter them--maybe clues lie within).

    -----

    # method1
    ############### ############### ############### ############### #########
    # get_password(se lf)
    ############### ############### ############### ############### #########
    def ask_for_passwor d(self, message):
    def cancel():
    passWindow.dest roy()
    def ok():
    # WORKS FINE!
    print passWindow.resu lt
    pw = passEntry.get()
    if len(pw):
    passWindow.resu lt = pw
    passWindow.dest roy()
    message = "\n" + message + "\n"
    # make a new top level for the pass window
    passWindow = Toplevel(self.g et_mainWindow() )
    passWindow.resu lt = None
    # title it something meaningful
    passWindow.titl e("passerby - Password Entry")
    # - will use the main view (view #0) to own the password entry
    # this will help the user remember what he's doing
    # - need to show the main view
    self.get_mainWi ndow().deiconif y()
    # - set ownership of the passWindow to main view
    passWindow.tran sient(self.get_ mainWindow())
    # setting the geometry of the passWindow
    # pass_x = anEvent.x_root - 50
    # pass_y = anEvent.y_root - 50
    passWindow.geom etry("400x180+5 0+50")
    # - don't let user change our beautiful window
    passWindow.resi zable(0,0)
    # the entry label and field
    passLabel = Label(passWindo w, text=message)
    passEntry = Entry(passWindo w)
    passEntry.confi gure(width=48)
    passEntry.confi g(show="*")
    passEntry.bind( "<Return>", ok)
    cancelButton = Button(passWind ow, text="Cancel", command=cancel)
    okButton = Button(passWind ow, text="OK", command=ok)
    passLabel.pack( )
    passEntry.pack( )
    cancelButton.pa ck()
    okButton.pack()
    passEntry.focus _set()
    passWindow.grab _set()
    self.get_tk().w ait_window(pass Window)
    return passWindow.resu lt

    -----

    # method2
    ############### ############### ############### ############### #########
    # get_password(se lf)
    ############### ############### ############### ############### #########
    def ask_for_passwor d(self, message):
    def cancel():
    passWindow.dest roy()
    def ok():
    # GETS ERROR!
    print result
    pw = passEntry.get()
    if len(pw):
    passWindow.resu lt = pw
    passWindow.dest roy()
    message = "\n" + message + "\n"
    # make a new top level for the pass window
    passWindow = Toplevel(self.g et_mainWindow() )
    result = None
    # title it something meaningful
    passWindow.titl e("passerby - Password Entry")
    # - will use the main view (view #0) to own the password entry
    # this will help the user remember what he's doing
    # - need to show the main view
    self.get_mainWi ndow().deiconif y()
    # - set ownership of the passWindow to main view
    passWindow.tran sient(self.get_ mainWindow())
    # setting the geometry of the passWindow
    # pass_x = anEvent.x_root - 50
    # pass_y = anEvent.y_root - 50
    passWindow.geom etry("400x180+5 0+50")
    # - don't let user change our beautiful window
    passWindow.resi zable(0,0)
    # the entry label and field
    passLabel = Label(passWindo w, text=message)
    passEntry = Entry(passWindo w)
    passEntry.confi gure(width=48)
    passEntry.confi g(show="*")
    passEntry.bind( "<Return>", ok)
    cancelButton = Button(passWind ow, text="Cancel", command=cancel)
    okButton = Button(passWind ow, text="OK", command=ok)
    passLabel.pack( )
    passEntry.pack( )
    cancelButton.pa ck()
    okButton.pack()
    passEntry.focus _set()
    passWindow.grab _set()
    self.get_tk().w ait_window(pass Window)
    return result

    --
    James Stroud, Ph.D.
    UCLA-DOE Institute for Genomics and Proteomics
    611 Charles E. Young Dr. S.
    MBI 205, UCLA 951570
    Los Angeles CA 90095-1570

Working...