如何在Python中为__init__中的方法声明一个全局变量集?
How to declare a global variable set to a method in __init__ in Python?
我是面向对象编程的新手,我在声明设置为 class 中的方法的全局变量时遇到了一些麻烦。我在 __init__
方法中声明了它们(我希望它们在 class 内部,而不是外部)和 self.censored_transcript = self.apply_filter()
,在这个文件中,一切正常。但是,当我在不同的 .py 文件中导入 class 并实例化 Profanity class 时,会发生以下错误: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)
您可以在 class 中的所有地方使用 self.censored_transcript
- self
指的是 class 的当前对象。您 class 的每个创建的对象都将存储自己的 self
可用变量集。
censored_transcript
没有 self
被视为常规局部变量,将为每次方法调用新创建。
错误原因:
当从外部 .py 文件调用方法 def apply_filter(self)
时,if __name__ == "__main__"
检查将为假,并且不会在该方法内部创建局部变量 censored_transcript
。因此,return censored_transcript
尝试 return 具有未分配值的局部变量并失败。
解决方案 1: in apply_filter(self)
您可以使用对象的 censored_transcript
而不是本地的来保存结果。只需在其前面附加 self.
即可。而且你不需要return这个方法的东西:
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)
我是面向对象编程的新手,我在声明设置为 class 中的方法的全局变量时遇到了一些麻烦。我在 __init__
方法中声明了它们(我希望它们在 class 内部,而不是外部)和 self.censored_transcript = self.apply_filter()
,在这个文件中,一切正常。但是,当我在不同的 .py 文件中导入 class 并实例化 Profanity class 时,会发生以下错误: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)
您可以在 class 中的所有地方使用 self.censored_transcript
- self
指的是 class 的当前对象。您 class 的每个创建的对象都将存储自己的 self
可用变量集。
censored_transcript
没有 self
被视为常规局部变量,将为每次方法调用新创建。
错误原因:
当从外部 .py 文件调用方法 def apply_filter(self)
时,if __name__ == "__main__"
检查将为假,并且不会在该方法内部创建局部变量 censored_transcript
。因此,return censored_transcript
尝试 return 具有未分配值的局部变量并失败。
解决方案 1: in apply_filter(self)
您可以使用对象的 censored_transcript
而不是本地的来保存结果。只需在其前面附加 self.
即可。而且你不需要return这个方法的东西:
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)