Home > OS >  return Empty list
return Empty list

Time:09-29

I have two classes in a different file, and I want to pass list values from one to another, but it gives me an empty list; my code is very long, so that I will provide an example.

Example: file1.py

class a:
   def __init__ (self):
     self.result = [] 
   def set_result(self, result_a):
      self.result.append(result_a)

file2.py

class b:
   def add_value_to_list():
     file1.a().set_result("add_value")

The code is to understand the idea. I want self.result returned all values that I added by using add_value_to_list() I want self.result = ["add_value"] that I passed from add_value_to_list()

CodePudding user response:

there are two problems with this, the first was fixed with an edit, but the second may be more structural

  • missing self arg to method
    methods normally need to be passed this as their first argument to access other methods and data attached to their instance of the class

  • class b creates a new instance of a every time add_value_to_list is called (a()), instead it should probably create a single instance of a during its init and refer to it on some self.

  • Related