[Tutor] variable existence q
Emile van Sebille
emile at fenx.com
Sun Aug 16 00:08:59 CEST 2015
On 8/15/2015 2:47 PM, Mark Lawrence wrote:
> On 15/08/2015 22:11, Peter Otten wrote:
>> Clayton Kirkwood wrote:
>>
>>> 10 top_directory = "/users/Clayton/Pictures"
>>>
>>> def override_defaults():
>>> 56 return( top_directory, filetypes, target_directory )
>>>
>>> 80 top_directory, filetypes, target_directory = override_defaults()
>>>
>>>
>>> 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
>>>
<snip>
> 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.
Emile
More information about the Tutor
mailing list