RGETLB RGETLB - 1 month ago 23
Python Question

NameError: name is not defined

Updated Code Snippet:
I have updated to the 'self.' convention instead of global, but still having the only value returned the empty string assigned on the class level, but if this is removed, an error is thrown.

class MainDialog(QWidget, qcDbWidget4.Ui_qcQueryWidget):

dbDir = ''
DbSelection = ''
dbPath = ''
TabSelection = ''
exportDir = ''
exportFileName = ''

def __init__(self, parent=None):
super(MainDialog, self).__init__(parent)
self.connect(self.dbDirSelect, SIGNAL("clicked()"), self.getDirFileNames)
self.connect(self.dbSelectButton, SIGNAL('clicked()'), self.selectDb)
self.connect(self.tabSelectButton, SIGNAL('clicked()'), self.selectTab)
self.connect(self.exportDirSelect, SIGNAL("clicked()"), self.getExportDir)
self.connect(self.exportButton, SIGNAL("clicked()"), self.setExportName)
self.connect(self.querySubmitButton, SIGNAL("clicked()"), self.setQueryParams)
self.connect(self.querySubmitButton, SIGNAL("clicked()"), self.dbQuery)

def getDirFileNames(self):
self.dbDir = str(QFileDialog.getExistingDirectory(self))

dbFileList = []
for root, dirs, files in os.walk(self.dbDir):
for file in files:
if file.endswith('.db'):


Answer Source

When you define variable on class level like

class A(object):
    c = ''

You can use 'c' as static class variable or instance variable (or both, but it's confusing). If you want to use it as static class variable you need to use it as A.c (static variable for whole class, accessible outside of instance methods). If you want to use it as instance variable you need to use self.c (own variable for every instance). c as variable in this case will not be defined and if you will try to use global c you should have NameError.