简体   繁体   English

类变量的未定义变量名称

[英]Undefined Variable Name for Class Variable

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. 我得到一个未定义的名称“ KMP”,我真的不明白为什么。 Could anyone help me in solving this error? 谁能帮助我解决这个错误?

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 . 在您的情况下,您尝试在定义KMP的代码之前创建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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM