【问题标题】:How to declare a global variable set to a method in __init__ in Python?如何在 Python 的 __init__ 中声明一个设置为方法的全局变量?
【发布时间】:2020-10-21 12:09:40
【问题描述】:

我是面向对象编程的新手,在声明设置为类中方法的全局变量时遇到了一些麻烦。我用self.censored_transcript = self.apply_filter()__init__ 方法中声明了它们(我希望它们在类内,而不是在类外),在这个文件中,一切正常。但是当我在不同的 .py 文件中导入类并实例化 Profanity 类时,会出现以下错误:UnboundLocalError: local variable 'censored_transcript' referenced before assignment。我已经对这个主题进行了相当多的研究,但找不到任何适合我的情况。 global 语句不起作用。谁能告诉我如何解决这个问题?非常感谢您!

文件1(要导入的文件),名为profanity_filter.py

from better_profanity import profanity


class Profanity(object):

    def __init__(self, transcript_filename, wordlist_filename):
        self.__transcript_filename = transcript_filename
        self.__wordlist_filename = wordlist_filename

        self.__transcript = self.__load_transcript()
        self.censored_transcript = self.apply_filter()

    def __load_transcript(self):
        f = open(self.__transcript_filename, "r")
        file = f.read()
        f.close()

        return file

    def apply_filter(self):
        if __name__ == "__main__":
            profanity.load_censor_words_from_file(self.__wordlist_filename)
            censored_transcript = profanity.censor(self.__transcript)

        return censored_transcript

文件2(测试文件):

from profanity_filter import Profanity


# Instantiate the Profanity class:
p = Profanity("sample_transcript.txt", "profanity_wordlist.txt")


# Apply the profanity filter.
censored_transcript = p.apply_filter()
print(censored_transcript)

【问题讨论】:

  • 你使用if __name__ == "__main__"的目的是什么?也许您正在寻找Singletone 架构师模式?

标签: python-3.x class oop global-variables


【解决方案1】:

您可以在班级内的所有地方使用self.censored_transcript - self 指的是班级的当前对象。您的类的每个创建的对象都将存储自己的self 可用变量集。

censored_transcript 没有self 被视为常规局部变量,将为每次方法调用新创建。

错误原因: 当方法 def apply_filter(self) 从外部 .py 文件中调用时,if __name__ == "__main__" 检查将为 false,并且不会在此方法内创建局部变量 censored_transcript。因此,return censored_transcript 尝试返回未赋值的局部变量并失败。

解决方案 1:apply_filter(self) 中,您可以使用对象的censored_transcript,而不是本地,来保存结果。只需在其前面附加self.。而且你不需要从这个方法中返回一些东西:

from better_profanity import profanity


class Profanity(object):

    def __init__(self, transcript_filename, wordlist_filename):
        self.__transcript_filename = transcript_filename
        self.__wordlist_filename = wordlist_filename

        self.__transcript = self.__load_transcript()
        self.apply_filter()  # changed

    def __load_transcript(self):
        f = open(self.__transcript_filename, "r")
        file = f.read()
        f.close()

        return file

    # changed
    def apply_filter(self):
        profanity.load_censor_words_from_file(self.__wordlist_filename)
        self.censored_transcript = profanity.censor(self.__transcript)

解决方案 2: 使用局部变量,但在没有 if 的情况下为其赋值。甚至可以省略这个局部变量:

from better_profanity import profanity


class Profanity(object):

    def __init__(self, transcript_filename, wordlist_filename):
        self.__transcript_filename = transcript_filename
        self.__wordlist_filename = wordlist_filename

        self.__transcript = self.__load_transcript()
        self.censored_transcript = self.apply_filter()

    def __load_transcript(self):
        f = open(self.__transcript_filename, "r")
        file = f.read()
        f.close()

        return file

    # changed
    def apply_filter(self):
        profanity.load_censor_words_from_file(self.__wordlist_filename)
        return profanity.censor(self.__transcript)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-30
    • 1970-01-01
    • 2011-03-22
    • 2011-02-12
    • 2012-11-11
    • 2019-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多