Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

My function works on its own but not callable from class

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

NameError: name 'plugins' is not defined

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading