aledj2
aledj2

Reputation: 53

How do I read a list that is located within another class?

In Python I have a for loop which calls a class, which in turn calls another class and so on, with classes manipulating data, performing sql inserts etc. The final class contains a list of all the files which have been created. I want to access this list from outside the class but I cannot work out how to!

(I know there is also a loop issue-will explain more below!)

A basic example is:

#A class to create the list
class Create_list():
    def list(self,j):
        l=j+1
        #pass this variable to another class, get_list
        Get_list().input(l)

#class get_list receives the number from create_list and appends it to mylist
class Get_list():
    def input(self,l):
        mylist=[]
        mylist.append(l)    
        #print mylist

# loop through a list of numbers and feed them into the create_list class
j=10
for k in range(j):
    Create_list().list(k)

#I want to access the list here. I have tried all of the below
readlist=Get_list().input().mylist # with ()
readlist=Get_list.input.mylist # without ()
x=Create_list() # create an object with class name
mylist=x.list().mylist #use above object

I have tried all the approaches in the last block of code.

I can't use the first two as the function list requires an input, which comes from the preceding class. (the error says that list() requires two arguments, only one is provided (self I assume)).

I have tried assigning the class to an object but this too does not work.

I realise that the for loop means that if I were to print mylist within def input there is only the value from that value of j.

I basically would like to access mylist, which has a list of values (l) from all of the values in j after that for loop has run.

Upvotes: 0

Views: 115

Answers (3)

syntonym
syntonym

Reputation: 7384

Another possibility is to return the list:

def add_one_to(j):
    l=j+1
    return(l)

def store(mylist, l):
    mylist.append(l)
    return(mylist)

Usage:

>>> mylist = []
>>> myintegerplusone = add_one_to(1)
>>> store(mylist, myintegerplusone)
>>> print(mylist)
[2]

In that case you could imagine a function as a craftsman, you give him something to fix/manipulate and he then returns the fixed/manipulated good back to you.

Upvotes: 1

chown
chown

Reputation: 52738

Lots of stuff is wrong here so I'll just show a simple way to do it:

class Create_list(object):
    def __init__(self):
        self.list = []


    def input_list(self, x):
        l = x + 1
        self.list.append(l)

j=10
cl = Create_list()
for k in xrange(j):
    cl.input_list(k)

print cl.list

Upvotes: 5

Fran Lupión
Fran Lupión

Reputation: 134

I think what you want is to store inside the class object the list using the "self" method and then access it from outside.

Try this code:

class CreateList():
    def __init__(self):
        self.list = []

if __name__ == "__main__":
    c = CreateList()
    c.list.append(4)
    print c.list

Upvotes: 0

Related Questions