Home > other >  My function works on its own but not callable from class
My function works on its own but not callable from class

Time:12-08

I've made a class as follow:

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

if I run the method function on its own and outside of the class it works totally fine, namely if I write the function as:

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

in the function alone I get rid of self and will just have voter_rep(loc, start_header, end_header) but when I want to call it from the class I do plugins.voter_rep(loc, start_header, end_header) which does not work, and it returns:

NameError: name 'plugins' is not defined

I wonder why is it that my function works on its own but not callable from the class?

CodePudding user response:

You can do

plugins = Plugins()
loc = #some val
start_header = #some val
end_header = #some val
plugins.voter_rep(loc, start_header, end_header)

As the error message shows, you are using small 'p' instead of capital. Also since it is not a static function, so it is not good to call it via class name.

CodePudding user response:

Plugins.voter_rep(loc, start_header, end_header)

Take note of the capital letter.

  • Related