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

Class that holds variables and has methods?

I have a class like this:

class ErrorMessages(object): 
    """a class that holds all error messages and then presents them to the user)"""
    messages= []
    userStrMessages= ""

    def newError(self, Error): 
        self.userStrMessages+= Error

    def __str__(self):
        if self.messages.count() != 0: 
            i=0
            for thing in self.messages: 
               self.userStrMessages += self.messages[i] + "\n"
               i+=1
        return self.userStrMessages

this doesn’t work when I call on it like this and wants 2 input variables, but that is just self and what i put into it?:

        ErrorMessages.newError(errormessage)

errormessage is a string

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

i have a (what i think is) a static class? (new to this and learned in swedish which makes this harder) it looks like this

class EZSwitch(object): 
    fileway= "G:/Other computers/Stationär Dator/Files/Pyhton Program/Scheman"
    fileway2= "Kolmården.txt"
    numberSchedules= 30

I thought i could make my errormessagclass like this but it also does things with methods. Like adds things to the list messages on newError. It doesnt seem to be possible or how would I do this?

>Solution :

If you want to call a method directly without making an object first then you’ll have to make your method newError() a class method and then call it as you mentioned above.

@classmethod
def newError(self, Error): 
    self.userStrMessages+= Error

ErrorMessages.newError(errormessage)

Otherwise you can create an object first and then call the method as:

err_object = ErrorMessage()
err_object.newError(errormessage)
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