Python cannot see global variable -


although specyfing variable global inside function this:

def secdownload(year, month):     import os     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 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) + '/' 

and import function , run in python ui (windows) this:

>>> df import secdownload >>> secdownload(2012,4) 

why when type variable target_dir in shell get:

>>> target_dir traceback (most recent call last): file "<pyshell#6>", line 1, in <module>     target_dir  nameerror: name 'target_dir' not defined 

how possible when state inside function variable global ?

functions work in context in created. is, globals work local module function created in.

for instance:

m.py:

def a(val):     global x     x = val 

main.py

from m import a(10) import m print(m.x) 

produces 10


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

jquery - Keeping Kendo Datepicker in min/max range -