如何更新python中的全局变量 [英] how to update global variable in python
问题描述
有点像这样:
list = [link1,link2,...]
def update() :
#代码更新列表
返回列表
def下载(列表):
#代码从链接下载
所以我希望更新功能每5天运行一次(我知道该怎么做)以及每天运行的下载功能。那么我怎样才能保持从update()静态返回列表作为全局列表,直到它再次更新?
编辑:
让我试着澄清:
我在星期一运行:
list = [ link1,link2]
def update():
#code to update list
return list# - > list = [link1,link2,link3]
def download(list):
#code从链接下载
这个工作正常,列表已更新并用于下载()。
我运行这个在星期二:
list = [link1,link2]
#update()不会今天运行,每5天运行
def update():
#code更新列表
返回列表# - > list = [link1,link2,link3]
def download(list):
#code从链接下载
我重新启动了我的代码,但现在列表中没有从星期一起的link3。
谢谢
使用全局
语句。但是对于可变对象,如果你在原地修改它们,则不需要 global
。
您可以使用 pickle
将您的列表存储在一个文件中。
lis = [link1)您可以在您想要使用它时加载列表并将其存储回去。 ,link2,...]
def update():
global lis
#do something
return lis
Pickle示例:
import pickle
def update():
lis = pickle.load(open(lis.pkl,rb))#加载列表
#用lis做某事#修改它
pickle.dump(lis,open(lis.pkl,wb))#save again
为了获得更好的效果,您还可以使用 cPickle 模块。
In python, i have a function that returns a list of the latest links(to folders) on a website. I also have another function that downloads the latest files from those folders. I plan to run this script everyday. I have a global list with the folder links that the download function accesses everytime it runs for the latest folders. I want to update that global list every five days and keep it static for the next 5 days i run the code until it updates again.
Its sort of like this:
list = ["link1", "link2",...]
def update():
#code to update list
return list
def download(list):
#code to download from links
So I want the update function to run every 5 days(I know how to do that) and the download function to run everyday. So how can i keep the list returned from update() static as the global list until it is updated again?
EDIT: Let me try to clarify:
I run this on a monday:
list = ["link1", "link2"]
def update():
#code to update list
return list #--> list = ["link1", "link2", "link3"]
def download(list):
#code to download from links
this worked fine, list was updated and used in download().
I run this on a Tuesday:
list = ["link1", "link2"]
#update() won't run today, only runs every 5 days
def update():
#code to update list
return list #--> list = ["link1", "link2", "link3"]
def download(list):
#code to download from links
I restarted my code, but now list doesnt have link3 from monday. How do i keep link3 in the list for the next 5 days until i update list again?
Thanks
Use global
statement. But there's no need of global
for mutable objects, if you're modifying them in-place.
You can use modules like pickle
to store your list in a file. You can load the list when you want to use it and store it back after doing your modifications.
lis = ["link1", "link2",...]
def update():
global lis
#do something
return lis
Pickle example:
import pickle
def update():
lis = pickle.load( open( "lis.pkl", "rb" ) ) # Load the list
#do something with lis #modify it
pickle.dump( lis, open( "lis.pkl", "wb" ) ) #save it again
For better performance you can also use the cPickle module.
这篇关于如何更新python中的全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!