Search code examples
pythonwxwidgets

Python global variable isspace() error


I define a global of sshIP along with others in Main

 foo,bar,sshIP = '','',''

using wx button event I define a function called load to test to see if sshIP has a value or if it is still '', from what I've seen I can do this with isspace(), however my implementation seems to always be met with a NameError that sshIP is not defined. load is a function of Main

def load(self,e):
    if sshIP.isspace(): 
        self.progressBox.AppendText("Cannot install, device not connected")
    else:
        self.installBtn.Enable()

Solution

  • Since Main is a function, those variables are not global unless you have marked them as such.

    def Main():
        global foo
        global bar
        global sshIP
    
        foo,bar,sshIP = '','',''
    

    But you might consider passing these variables as parameters to whatever you call next, rather than using globals.

    To test for a non-empty string you can just:

    if sshIP:
    

    since an empty string is False in a boolean context.