在Python中,我有一个for循环,它调用一个类,后者又调用另一个类,依此类推,类操作数据,执行sql插入等.最后一个类包含已创建的所有文件的列表.我想从课外访问这个列表,但我无法弄清楚如何!
(我知道还有一个循环问题 - 将在下面解释更多!)
一个基本的例子是:
#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
Run Code Online (Sandbox Code Playgroud)
我已经尝试了最后一段代码中的所有方法.
我不能使用前两个,因为函数列表需要一个输入,它来自前面的类.(错误说list()需要两个参数,只提供一个参数(我自己假设)).
我已经尝试将类分配给一个对象,但这也不起作用.
我意识到for循环意味着如果我要mylist在def input其中打印,那么只有该值的值j.
我基本上想要访问mylist,其中有一个值列表(l)来自jfor循环运行之后的所有值.
这里有很多东西是错的,所以我只想说明一个简单的方法:
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
85 次 |
| 最近记录: |