0

I have a class with a list. I am trying to return the list in a different class, but it returns the list as the original empty list instead of the new list with elements (SOLVED!!).

class Whatever:
    def __init__(self,thing):
        self.list=[]
        self.thing=thing

    def list_append(self):
        df1=pd.read_csv('job.py')
        df2=pd.read_csv('home.py')
        self.list.append([df1,df2])
        return self.list
    def new_function(self):
        return self.list

function=Whatever('thing1')
function_output=function.new_function()
print(function_output)

4
  • 1
    This code never calls list_append(), so of course self.list is still in its original empty state. Commented Nov 25, 2019 at 22:25
  • I am trying to send the results of the list_append() function to the new_function(). I dont exactly know how to do this. Commented Nov 25, 2019 at 22:29
  • @NickBosio You need to call a function for it to run to get results from it. Commented Nov 25, 2019 at 22:31
  • I don't understand what you mean by "I am trying to send the results of the list_append() function to the new_function()", because looking at the code, I can see that list_append() is never called. Commented Nov 25, 2019 at 22:35

2 Answers 2

1

There are many errors in your code:

  1. First, you should write def __init__(self, thing1): instead of def __init__:
  2. You have to call the method list_append() to fill your list.

Here is the working code:

class Whatever:
    def __init__(self, thing1):
        self.list=[]
        self.thing=thing1

    def list_append(self):
        df1=pd.read_csv('job.py')
        df2=pd.read_csv('home.py')

        self.list.append([df1,df2])
        return self.list

    def new_function(self):
        self.list_append() # to call the above method
        return self.list

function=Whatever(thing1)
function_output=function.new_function()
print(function_output)

Test: Let assume df1 = "hello", df2 = "world" and thing1 = "good". The above code will produce the result [['hello', 'world']].

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! It works now, but what if I want to loop through self.list. For some reason, my loop output still gives me a list instead of a dataframe. I am going to re-edit my code to show what I am trying to do.
0

You are not calling it in a different class but in a different method, but this may be just a terminology error, so nothing serious. But the most important error will probably be this:

You are trying to call output of the list_append function, but you never call it, so you have to do in the new function something like this:

def function(foo):
    return(foo)

def new_function(foo_foo):
    function(foo)

2 Comments

Lol. Ignore the __init__(self) portion. I thought the return self.output of a method can be used throughout the code.
Sorry, my mistake, I tried to help but then I realized I am writing nonsense, my apologies, I deleted the rubbish part

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.