[Tutor] variable existence q
Emile van Sebille
emile at fenx.com
Sun Aug 16 00:56:05 CEST 2015
On 8/15/2015 3:38 PM, 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.
Actual assignment isn't required to make it a local variable -- only the
fact that it appears on the left hand side of an assignment statement
with the function.
> 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.
Not any more -- it's a local variable because the assignment, while not
executed, exists.
> If that is changed inside the function, doesn't it change
> the global?
Only if you include the globals statement before the variable is referenced.
Emile
More information about the Tutor
mailing list