Python keep class in memory

Viewed 515

I'm fairly new to python so please correct me if my question does not make sens.

I am writing a small script, which should check some data and log those that failed. To have a better overview I am maintaining two files. One with my main function and the other with the other functions.

To log my failed data I have this class:

 class OutputLog():
  def __init__(self):
    self.errorLog = []

  def log(self, item: str):    #write data to errorLog if called
    self.errorLog.append(item)
    self.errorLog.append('\n')

  def write(self):             #return errorLog
    return self.errorLog

In my check function I call OutputLog.log() like this:

####### Check Data ########
class Check:
  def __init__(self, sortedData):
    self.sortedData = sortedData

## Check if name got spaces and log them
  def nameSpace(self):
    out = OutputLog()
    for column in self.sortedData:
      item = column[0]
      if ' ' in item:
        out.log(item)      #Call Output log
      else:
        continue

In my main function, which is in the other .py file (don't really know if this makes a huge difference) I want to get the error log like this:

import my_functions

def main():
  out = my_functions.OutputLog()
  c = my_functions.Check()
  c.nameSpace()
  print(out.write())

But like this my error log is still empty. Is there a way how I can only have one instance of the OutputLog class or do I need to make the errorLog variable public?

If anyone knows a better way to do this I would love to hear your recommendations.

1 Answers

Your out variables are in different namespaces, and thus does not refer to each other. What you can do though, is change the errorLog variable to be a class-attribute instead of an instance-attribute.

class OutputLog:
    errorLog = []

    # Creates a classmethod that can be called without a create instance.
    @classmethod
    def log(self, item: str): 
        OutputLog.errorLog.append(item)

    # Creates a classmethod that can be called without a create instance.
    @classmethod
    def write(self):
        return OutputLog.errorLog

class Check:
    def __init__(self, sortedData):
        self.sortedData = sortedData

    def nameSpace(self):
        for column in self.sortedData:
            item = column[0]
            if ' ' in item:
                OutputLog.log(item)  # Calling the class-method directly, not an instance
            else:
                continue

You didn't provide any data to test the Check class, so I created my own data:

import my_functions

def main():
    c = my_functions.Check([" " for i in range(10)])
    c.nameSpace()
    print(my_functions.OutputLog.write()) # Output: [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']

main()

What I've done here is change the references from your out instances of OutputLog into the class itself, and changed the methods to be classmethods so that you can call them freely.


As a side-note, you should follow PEP 8 and use 4-spaced indentation in your code to create better readability and standardized code so that it's easier to collaborate with others in the future.

Related