尽管我将变量指定为函数中的global
,如下所示:
def SECdownload(year, month):
import os
from urllib.request import urlopen
root = None
feedFile = None
feedData = None
good_read = False
itemIndex = 0
edgarFilingsFeed = 'http://www.sec.gov/Archives/edgar/monthly/xbrlrss-' + str(year) + '-' + str(month).zfill(2) + '.xml'
return edgarFilingsFeed
#print( edgarFilingsFeed ) #from the slides
if not os.path.exists( "sec/" + str(year) ):
os.makedirs( "sec/" + str(year) )
if not os.path.exists( "sec/" + str(year) + '/' + str(month).zfill(2) ):
os.makedirs( "sec/" + str(year) + '/' + str(month).zfill(2) )
global target_dir
target_dir = "sec/" + str(year) + '/' + str(month).zfill(2) + '/'
然后我导入函数,然后在Python UI(Windows)中运行它,如下所示:
>>> from df import SECdownload
>>> SECdownload(2012,4)
为什么在Shell中键入变量target_dir
时会得到:
>>> target_dir
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
target_dir
NameError: name 'target_dir' is not defined
当我在函数中清楚地声明variable
是global
时,这怎么可能呢?你知道吗
处理全局变量的代码无法访问,因为这行:
函数在创建它们的上下文中工作。也就是说,它们使用的任何全局变量都是创建函数的模块的本地变量。你知道吗
例如:
男/女:
你知道吗主.py你知道吗
产生
10
相关问题 更多 >
编程相关推荐