Search code examples
pythonnameerror

NameError: name 'of class' is not defined


I wrote a class (searcher)for searching for a string of words in a data base but when i want to execute my script i get this error:

NameError: name 'searcher' is not defined

my code:

class searcher:
    def __init__(self, dbname):
        self.con = sqlite3.connect(dbname)

    def __del__(self):
        self.con.close()

    def getmatchrows(self,q):
        fieldlist = 'w0.urlid'
        tablelist = ''
        clauselist = ''
        words = q.split(' ')
        tablenumber = 0
        wordids = []

        for word in words:
            wordrow = self.con.execute("select rowid from wordlist where word = '%s'" %word).fetchone()
            if wordrow !=None:
                wordid = wordrow[0]
                wordids.append(wordid)
                if tablenumber > 0:
                    tablelist+=','
                    clauselist+=' and '
                    clauselist+='w%d.urlid=w%d.urlid and ' % (tablenumber -1, tablenumber)
                fieldlist+=',w%d.location'% tablenumber
                tablelist+='wordlocation w%d'% tablenumber
                clauselist+='w%d.wordid = %d' % (tablenumber,wordid)
                tablenumber+=1

        query = 'select %s from %s where %s' % (fieldlist,tablelist,clauselist)
        cur = self.con.execute(query)
        rows = [row for row in cur]

        return rows,wordids

    wordsearch = searcher('searchindex.db')
    print  wordsearch.getmatchrows('indie music')

what am I doing wrong?!!


Solution

  • The last two lines of your code are indented, and so they belong to the class searcher: block. However, the searcher class does not exist until after this block ends, and so attempting to refer to searcher in wordsearch = searcher('searchindex.db') fails.

    Unindent the last two lines.