None

Brian Kelley bkelley at wi.mit.edu
Thu Oct 25 17:02:00 CEST 2001


David Bolen wrote:

>"Larry Whitley" <ldw at us.ibm.com> writes:
>
>>That's it!  Here's the offending code, a little earlier in process().
>>
>>        elif pc.command == "dual": # command was a dual address cycle
>>            pc.command, None, None = tr.decodeCommand()
>>
>>Looks like it considers None a variable though it is not under the elif that
>>used it.
>>
>
>Yes, local variable determination is a static compile-time operation,
>based on compiling an assignment, and not runtime based.  But the
>error is runtime based if the variable gets used in the code path
>before being initialized.
>
Yeah.  I got bit by this in almost my first python program.  It was a 
little confusing.  Consider the following code:

def bad_len():
    a = [1,2,3,4,5]
    len = len(a)
  
Traceback (most recent call last):
  File "<stdin>", line 5, in ?
  File "<stdin>", line 3, in bad_len
UnboundLocalError: local variable 'len' referenced before assignment
 

It sure teaches you not to use things like "min", "max" and "len" as 
variable names really quick...  

I always wanted to go into the error messages and change 
UnbondLocalError for built in names to be a little more explicit about 
likely errors.  But once you learn what's going on then there's no need 
anymore.


Brian Kelley
Whitehead Institute for Biomedical Research





More information about the Python-list mailing list