(no subject)
Jan. 4th, 2019 01:26 amI fixed a bug today which was mildly interesting. It was caused by a local variable in a function having the same name as an unrelated imported module which was referenced earlier in the function. So the code was of this shape:
When
Of course, the answer is that the later reassignment was responsible. Python (like most programming languages, I believe) allocates local variables on the call stack, so it has to know what local variables there are as it calls the function, before it executes the function body. So it effectively does an initial pass over the body looking for assignments, and notices the
Now I see why C89 requires you to put all variable declarations at the start of a function! OK, I don't know if this was ever the actual rationale behind that restriction, but it does seem like a good reason for it. The C89 restriction makes it clear that local variable names have to be valid across the whole body of a function, not just from the line on which they are declared onwards.
import x
...
def f():
x.g()
...
x = a
When
g was called, an error occurred with the message "local variable x referenced before assignment". Now when I was debugging this, all I saw at first was the x.g() line (since that's the line the error occurred at), and I scrolled up, trying to find where the x variable had been defined, eventually finding that x was the name of the module. This left me rather confused, since I couldn't imagine how the module name x was getting interpreted as the name of a local variable when it hadn't been reassigned at any point earlier in the code.Of course, the answer is that the later reassignment was responsible. Python (like most programming languages, I believe) allocates local variables on the call stack, so it has to know what local variables there are as it calls the function, before it executes the function body. So it effectively does an initial pass over the body looking for assignments, and notices the
x = a line and rebinds the name x to a local variable, before it executes the x.g() line.Now I see why C89 requires you to put all variable declarations at the start of a function! OK, I don't know if this was ever the actual rationale behind that restriction, but it does seem like a good reason for it. The C89 restriction makes it clear that local variable names have to be valid across the whole body of a function, not just from the line on which they are declared onwards.
no subject
Date: 2019-01-04 02:39 am (UTC)package throwaway; import java.io.File; public class VarRef{ public static int x=5; public static void main(String[] args){ System.out.println(File.separator); //prints backslash (on Windows) int File=4; System.out.println(File); //prints 4 System.out.println(x); //prints 5 int x=2; System.out.println(x); //prints 2 } }Of course, the "File" thing is a contrived example that violates Java conventions (classes start with a capital letter, local vars with lowercase) so cases like that one would be basically nonexistent in real life.
no subject
Date: 2019-01-04 10:22 pm (UTC)(Actually, functions can be first-class objects in PHP. Functions declared in the normal way aren't, but there are anonymous function expressions whose values are first-class objects, and can be assigned to variables. That's PHP for ya :) )
no subject
Date: 2019-01-04 07:51 am (UTC)nooo