New to python and I found some weird (as a non-python dev) behavior in the way it handles scopes.
I have a code that parses arguments and uses them via argparser.
Shortened basic version of what I am doing:
import ...
def parser():
parser = argparse.ArgumentParser(description='Do stuff')
parser.add_argument('-key', dest='key', action='store', required=True)
parser.add_argument('-start_date', dest='start_date', action='store', required=True)
parser.add_argument('-end_date', dest='end_date', action='store')
return parser.parse_args()
args = get_parser()
client_id = args.key
distance_in_meters = 5000
sdate = args.start_date
start_date = datetime(int(sdate[0:4]),int(sdate[4:6]),int(sdate[6:8]),int(sdate[8:10]))
end_date = datetime(datetime.now().year,datetime.now().month,datetime.now().day,datetime.now().hour,datetime.now().minute)
if args.end_date:
end_date = datetime(int(args.end_date[0:4]),int(args.end_date[4:6]),int(args.end_date[6:8]),int(args.end_date[8:10]))
def search():
# print example for the sake of stack overflow
print "Kicking off search."
print "Client ID:", client_id
print "Distance in Meters:", distance_in_meters
print "Start Date:", start_date
...
search()
This will print everything before erroring out on print "Start Date:", start_date
which is caused by UnboundLocalError: local variable 'start_date' referenced before assignment
This would normally make sense to me since I didn't set any global
variables however,
- why was it able to print the other variables?
- why does changing the variable to
sdate
instead ofstart_date
work?
I know that it is able to be printed because if I move
sdate = args.start_date
start_date = datetime(int(sdate[0:4]),int(sdate[4:6]),int(sdate[6:8]),int(sdate[8:10]))
and put it inside search():
it will print.
Based on this question: "What is the scope of variables in JavaScript?" my code should work fine.
UPDATE
I think start_date
/end_date
might be reserved variables?
I thought maybe the start_date
in my parser()
was somehow the conflicting variable. So I changed start_date = ...
to start_date1 = ...
and magically print "Start Date:", start_date1
seems to work.
However, I decided if that doesn't work then key
(which is in the parser) should NOT work either. So I changed the code start_date1 = ...
to key = ...
and surprisingly `print "Start Date:", key" still works.
So I figured out a solution but I am still confused. I don't see start_date on the list of reserved variables for python.