我的函数独立运行,但无法从 class 调用
My function works on its own but not callable from class
我做了一个class如下:
class Plugins:
def __init__(self):
pass
def voter_rep(self, loc, start_header, end_header):
self.loc = loc
ocr_xml = AbbyyXML(loc)
xml_doc = XMLDoc(ocr_xml, CONSTANTS)
xml_doc.split_words("", False)
self.start_header = start_header
self.end_header = end_header
header_pages = xml_doc.se_page(start_header, end_header)
## and stuff
voter_dict = {'Voter':[], 'Record_Key':[], 'Comments':[]}
## and stuff
return voter_dict, rep_dict
如果我 运行 方法函数本身和 class 之外它工作得很好,也就是说,如果我把函数写成:
def voter_rep(loc, start_header, end_header):
ocr_xml = AbbyyXML(loc)
xml_doc = XMLDoc(ocr_xml, CONSTANTS)
xml_doc.split_words("", False)
header_pages = xml_doc.se_page(start_header, end_header)
## and stuff
voter_dict = {'Voter':[], 'Record_Key':[], 'Comments':[]}
## and stuff
return voter_dict, rep_dict
单独在函数中我摆脱了 self
并且只有 voter_rep(loc, start_header, end_header)
但是当我想从 class 调用它时我做 plugins.voter_rep(loc, start_header, end_header)
不起作用,它 returns:
NameError: name 'plugins' is not defined
我想知道为什么我的函数可以自己运行但不能从 class 调用?
Plugins.voter_rep(loc, start_header, end_header)
注意大写字母。
你可以做到
plugins = Plugins()
loc = #some val
start_header = #some val
end_header = #some val
plugins.voter_rep(loc, start_header, end_header)
如错误消息所示,您使用的是小 'p' 而不是大写。另外由于它不是静态函数,所以通过 class name.
调用它是不好的
我做了一个class如下:
class Plugins:
def __init__(self):
pass
def voter_rep(self, loc, start_header, end_header):
self.loc = loc
ocr_xml = AbbyyXML(loc)
xml_doc = XMLDoc(ocr_xml, CONSTANTS)
xml_doc.split_words("", False)
self.start_header = start_header
self.end_header = end_header
header_pages = xml_doc.se_page(start_header, end_header)
## and stuff
voter_dict = {'Voter':[], 'Record_Key':[], 'Comments':[]}
## and stuff
return voter_dict, rep_dict
如果我 运行 方法函数本身和 class 之外它工作得很好,也就是说,如果我把函数写成:
def voter_rep(loc, start_header, end_header):
ocr_xml = AbbyyXML(loc)
xml_doc = XMLDoc(ocr_xml, CONSTANTS)
xml_doc.split_words("", False)
header_pages = xml_doc.se_page(start_header, end_header)
## and stuff
voter_dict = {'Voter':[], 'Record_Key':[], 'Comments':[]}
## and stuff
return voter_dict, rep_dict
单独在函数中我摆脱了 self
并且只有 voter_rep(loc, start_header, end_header)
但是当我想从 class 调用它时我做 plugins.voter_rep(loc, start_header, end_header)
不起作用,它 returns:
NameError: name 'plugins' is not defined
我想知道为什么我的函数可以自己运行但不能从 class 调用?
Plugins.voter_rep(loc, start_header, end_header)
注意大写字母。
你可以做到
plugins = Plugins()
loc = #some val
start_header = #some val
end_header = #some val
plugins.voter_rep(loc, start_header, end_header)
如错误消息所示,您使用的是小 'p' 而不是大写。另外由于它不是静态函数,所以通过 class name.
调用它是不好的