[Tutor] variable existence q

Clayton Kirkwood crk at godblessthe.us
Sun Aug 16 00:38:31 CEST 2015

top_directory = "/users/Clayton/Pictures"
target_directory = top_directory      #directory we are checking
filetypes = ('jpg', 'png', 'avi', 'mp4', 'mov', 'bmp')


def override_defaults():
    with open( user_preferences ) as f:
        for line in f.readline():
            llist = line.split()
            if llist[0] == '#':   #comment line to ignore
            elif llist[0] == 'top_directory':
                if len(llist) == 1:
                    top_directory = llist[1]
            elif llist[0] == 'target_directory':
                if len(llist) == 1:
                    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
                    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
                filetypes.append(llist[0:]) #assume line contains 0,
consumes blank lines, or more media files w/wo leading dot
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

> > 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
> 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?


> Emile
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor

More information about the Tutor mailing list