Question

Hello im having a little bit of trouble with a script I just wrote. Im receiving a self is not defined error. Though im sure I have it defined. Am i missing something? Any advise would be greatly appreciated. Thank you

Error

 File "door_controllerTEST_V4_RFID.py", line 69, in <module>
    class RfidFileAuthenticator:

  File "door_controllerTEST_V4_RFID.py", line 76, in RfidFileAuthenticator
    print "reading from " + self.filename + " file"

NameError: name 'self' is not defined

Script

class RfidInput:
    def getInput(self):
        print "waiting for tag"
        tag = raw_input()
        return AuthToken(None,tag)

class RfidFileAuthenticator:
    filename = "tags.txt"
    tags = dict()
    def __init__(self):
        self.readFile()
    def readfile(self):
        secrets = open(self.filename, 'r')
    print "reading from " + self.filename + " file"
    for line in secrets:
            line = line.rstrip('\n')
            id, tag = line.split(',')
            self.tags[tag] = id
    def check(self,token):
        print "checking if " + token.secret + " is valid"
        if token.secret in self.tags:
            print "tag found belonging to: " + self.tags[token.secret]
            return True
        else: "tag not found"
        print
        return False

The indentations for my lines were wrong though after correting my lines im getting a instance has no attribute 'readFile"

  File "door_controllerTEST_V4_RFID.py", line 99, in <module>
    main()

  File "door_controllerTEST_V4_RFID.py", line 92, in main
    authenticator = RfidFileAuthenticator()

  File "door_controllerTEST_V4_RFID.py", line 73, in __init__
    self.readFile()

AttributeError: RfidFileAuthenticator instance has no attribute 'readFile'

How my script looks now

class RfidInput:
    def getInput(self):
        print "waiting for tag"
        tag = raw_input()
        return AuthToken(None,tag)

class RfidFileAuthenticator:
    filename = "tags.txt"
    tags = dict()
    def __init__(self):
        self.readFile()
    def readfile(self):
        secrets = open(self.filename, 'r')
        print "reading from " + self.filename + " file"
        for line in secrets:
                line = line.rstrip('\n')
                id, tag = line.split(',')
                self.tags[tag] = id
    def check(self,token):
        print "checking if " + token.secret + " is valid"
        if token.secret in self.tags:
            print "tag found belonging to: " + self.tags[token.secret]
            return True
        else: "tag not found"
        print
        return False
Was it helpful?

Solution

It looks like the indentation is wrong. All lines in the readfile method including and after print "reading from " + self.filename + " file" should be shifted one indentation level to the right.

Update. The reason for the new error is that you define readfile, but call readFile (with a capital F). Python is a case-sensitive language, you must be consistent.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top