> -----Original Message----- > From: Tutor [mailto:tutor-bounces+crk=godblessthe...@python.org] On > Behalf Of Mark Lawrence > Sent: Saturday, August 15, 2015 4:05 PM > To: tutor@python.org > Subject: Re: [Tutor] variable existence q > > On 15/08/2015 23:38, Clayton Kirkwood wrote: > > top_directory = "/users/Clayton/Pictures" > > target_directory = top_directory #directory we are checking > > filetypes = ('jpg', 'png', 'avi', 'mp4', 'mov', 'bmp') > > > > imports... > > > > def override_defaults(): > > with open( user_preferences ) as f: > > for line in f.readline(): > > llist = line.split() > > if llist[0] == '#': #comment line to ignore > > continue > > elif llist[0] == 'top_directory': > > if len(llist) == 1: > > pass > > else: > > top_directory = llist[1] > > elif llist[0] == 'target_directory': > > if len(llist) == 1: > > pass > > else: > > target_directory = llist[1] > > else: #assume only filetypes now or until next comment or > > other keyword > > if llist[0] == 'filetypes': #allow keyword w/wo > > following types > > if llist.length() == 1: > > continue #assume user plans either not > > interested in types or types coming on later line > > llist.pop([0]) #skip keyword and start > > recording > > filetypes.append(llist[0:]) #assume line contains 0, > > consumes blank lines, or more media files w/wo leading dot > > continue > > 56 return( top_directory, filetypes, target_directory ) > > 80 top_directory, filetypes, target_directory = override_defaults()> > > > > The error message again is: > > File "C:/Users/Clayton/python/find picture duplicates/find picture > > duplicates", line 80, in <module> > > top_directory, filetypes, target_directory = override_defaults() > > File "C:/Users/Clayton/python/find picture duplicates/find picture > > duplicates", line 56, in override_defaults > > return( top_directory, filetypes, target_directory ) > > UnboundLocalError: local variable 'top_directory' referenced before > > assignment > > > >>> Your explanation doesn't make any sense to me. I'd have thought > >>> that having assigned top_directory at line 10, but then trying to > >>> reassign it at line 80, means that the function now knows nothing > >>> about it, hence the error. > >> > >> Assigning to a variable inside a function makes that variable local, > >> which > > must > >> have happened as per the error message: > >> UnboundLocalError: local variable 'top_directory'... > >> > >> As Peter noted, somewhere within override_defaults there's an > >> assignment to it. Changing to > >> def override_defaults(top_directory=top_directory): > >> should initialize it in case the assignment path isn't processed. > > > > Above is the actual code. The file /user..../user preferences exists > > but is empty. Defaults are at the top. For what it is worth, the > > debugger stopped in the function shows the values stated as the > > defaults at the top. If I understand correctly, the readline() would > > drop out, but even if it doesn't no assignments would be made for > > top_directory or target_directory. I thought that top_directory was > > global to this file. I am hearing that it doesn't matter whether the > > assignment is above or below the function definition. I should be able > > to use the tuple for the results of the call, right? In this case, no > > assignment was made. If I understand, the function sees the global. If > > that is changed inside the function, doesn't it change the global? > > > > Crk > > You are trying to change it at line 80. Do you really want to do that? > If no I suggest you spell it TOP_DIRECTORY to indicate that it is a constant > that should not be changed.
No, I am saying at the top that the defaults are set, and if running the override_defaults changes them, then they get changed. I have defaults, and I allow the user to override them. crk > > -- > My fellow Pythonistas, ask not what our language can do for you, ask what > you can do for our language. > > Mark Lawrence > > _______________________________________________ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor