0

Undefined Variable Name seems to be a common problem and I've followed the exact process as some of the examples on here, but I have not had any success.

From what I understand, you have to make a class if you have multiple functions, and then make an instance of the class to call these methods.

Here is my pseudo-code:

start = KMP()
start.read()


class KMP:

     def read(self):
         Text = "AGABBBACC"
         Pattern = "BBB"
         result = self.kmp(self, Pattern, Text)

     def kmp(self, Pattern, Text):
         ........
         ........
         return self.numOcc`

I'm getting a undefined name 'KMP', and I really don't understand why. Could anyone help me in solving this error?

2
  • What line does this error occur on? Commented Mar 13, 2017 at 17:12
  • @AlexanderRD it appeared at the very start of the code, start = KMP() Commented Mar 13, 2017 at 17:17

1 Answer 1

2

You have to define something before you use it. In your case you're trying to create an instance of the class KMP before the code that defines KMP.

You need to move your first statement after the point in which you define the class.

class KMP:

     def read(self):
         Text = "AGABBBACC"
         Pattern = "BBB"
         result = self.kmp(self, Pattern, Text)

     def kmp(self, Pattern, Text):
         ........
         ........
         return self.numOcc`

start = KMP()
start.read()
Sign up to request clarification or add additional context in comments.

2 Comments

Oh such a simple solution! I am used to Java-like implementation where we write main methods at the time of the code. Am I correct in adding my self statements? I know that I have to write result = self.kmp..." based on other answers but I'm unsure if I have to use them in the arguments as well. I've tried to research this but have not seen as exact answer.
If you prefer, you can define a main function at the top for your main logic, but you still need to add a call to main at the end of the script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.