Python{NameError}名称“self”未在_init中定义__

2024-09-29 17:21:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在制作一个小型python脚本/程序,用于根据过滤关键字从某医学院提取考试问题

它一直运作得很好。我已经添加了越来越多的功能,提高了兼容性,并进行了一些调试。然而,现在脚本突然一直返回零个问题,当进入调试模式时,我发现在类__init__函数中的一行上出现了一个非常奇怪的NameError,它以前工作得很好。我不确定是什么触发了它,尽管IIRC我在代码的这一部分做了一些小的调整

在调试器中观察有问题的变量self.rawQuestions时,它似乎工作正常,直到某一行代码出现NameError

同样值得注意的是,我是一个编程新手,所以如果这是一个非常愚蠢的错误,并且有一个明显的解决方案,我不会感到惊讶,如果是这样的话,对不起。但是当我在google/stackoverflow中搜索类似的错误时,几乎所有的问题都使用了__init__类之外的self关键字,这是不同的

下面是一个小摘录,其中删除了不相关的代码,错误发生在self.rawQuestions = [x for x...:

class Exam:
    class Course:
        ...
    class Question:
        ...
    def __init__(self, text, path):
        # Add text to text string
        self.text = text

        # Split text into questions
        if re.search(r"(Q|q)uestion", self.text):
            self.rawQuestions = self.text.rsplit("Question")
        elif re.search(r"(F|f)råga", self.text):
            self.rawQuestions = re.split(r"(?:F|f)råga(?=\s)", self.text)
        
        ### ERROR IS ON LINE BELOW ###
        # Filter out any questions containing only "Orzone..."
        self.rawQuestions = [x for x in self.rawQuestions if not re.search(r"^(?<!\w)\s*\d*\s*Orzone\s*AB\s*Gothenburg\s*www\.orzone\.com.*$", x, flags=re.IGNORECASE|re.DOTALL)]

        # Filter out questions containing a question, but with an "Orzone.. or Course at the end"
        self.rawQuestions = [re.sub(r"\sOrzone\s*AB\s*Gothenburg\s*www\.orzone\.com.*$", "", x) for x in self.rawQuestions]

        # Delete course name and semester from questions then filter out empty questions
        self.rawQuestions = [re.sub(rf"\s*[\w ]*{self.course.searchterm}.*[vh]t-?\d\d.*$", "", x) for x in self.rawQuestions]

        self.rawQuestions = [x for x in self.rawQuestions if len(x) < 1]

        # Make a list of question objects using the questions extracted using split
        self.questions = []
        for question in self.rawQuestions:
            self.questions.append(self.Question(question, self))

Tags: 代码textinselfreforsearchif
1条回答
网友
1楼 · 发布于 2024-09-29 17:21:46

这里的问题应该是您的'\uuuu_uu_u_u_u'方法上的缩进。尝试如下更改:

class Exam:
    def __init__(self, text, path):
        # Add text to text string
        self.text = text

        # Split text into questions
        if re.search(r"(Q|q)uestion", self.text):
            self.rawQuestions = self.text.rsplit("Question")
        elif re.search(r"(F|f)råga", self.text):
            self.rawQuestions = re.split(r"(?:F|f)råga(?=\s)", self.text)

        # Filter out any questions containing only "Orzone..."
        self.rawQuestions = [x for x in self.rawQuestions if not re.search(r"^(?<!\w)\s*\d*\s*Orzone\s*AB\s*Gothenburg\s*www\.orzone\.com.*$", x, flags=re.IGNORECASE|re.DOTALL)]

        # Filter out questions containing a question, but with an "Orzone.. or Course at the end"
        self.rawQuestions = [re.sub(r"\sOrzone\s*AB\s*Gothenburg\s*www\.orzone\.com.*$", "", x) for x in self.rawQuestions]

        # Delete course name and semester from questions then filter out empty questions
        self.rawQuestions = [re.sub(rf"\s*[\w ]*{self.course.searchterm}.*[vh]t-?\d\d.*$", "", x) for x in self.rawQuestions]

        self.rawQuestions = [x for x in self.rawQuestions if len(x) < 1]

        # Make a list of question objects using the questions extracted using split
        self.questions = []
        for question in self.rawQuestions:
            self.questions.append(self.Question(question, self))

    class Course:
        ...
    class Question:
        ...

相关问题 更多 >

    热门问题