tsvs = glob.glob(sys.argv[1])
class StatFind:
# This is the instance initialiser.
def __init__(self,tsvs):
self.tsvs=tsvs
# here we define every instance of StatFind to have an attribute
# called 'finallist' which will be accessible by all methods
self.finallist = []
# We do our initialisation here, when we initialise our object,
# instead of in a separate method.
for path in self.tsvs:
finalist.append(do_something(path))
def inserttomongo(self):
# The 'self' parameter is automagically set up by python to
# refer to the instance when 'insertmongo()' is called on an instance
# for example, myInstance.insertmongo(), in which case
# self will be 'myInstance'
mongo=pymongo.Connection('localhost')
mongo_db=mongo['sample']
mongo_collection=mongo_db['users']
for dictvalue in self.finallist:
insert_id=mongo_collection.insert(dictvalue)
s=StatFind(tsvs) #This will call __init__() behind the scenes.
s.inserttomongo()
请阅读the Python tutorial尤其是the section about classes。在
你会在网上找到很多很棒的oopython教程,比如here或here。在
获取函数的返回值
例如,通过在命令行上运行python来使用解释器:
^{pr2}$清除代码:
你的代码没有意义。在
nmaidcount().finallist
?在finallist
?在if True
?永远都是这样!在我想你的意思是:
相关问题 更多 >
编程相关推荐