0 what "x=10" does
Variable declaration (or definition, allocation) and assignment are two different things. Let's take C code as example here.
int x; // this is declaration
x = 10; // this is assignment
It's very clear whether a statement is declaration or assignment. Even with sugar synx like below
int x = 10;
It's still clear that this statement is to create a completely new variable x.
combination of declaration and assignment is good, but it may be confusing!
In some languages including Python, it's impossible to tell whether a statement is to create a new variable or assign an existing variable without context.
e.g. Python code
x = 99
We cannot tell if this statement is to create a new variable x and set its value to 99 OR to assign 99 to an existing variable x. The exact meaning depends on contexts.
- if a variable named "x" can be found, then this line will not create a new variable.
- if a vairable named "x" cannot be found, then this line will create a new variable.
1 Python doesn't have variable declaration
1.1 variable resolvement depends on read or write
The way to find "x" depends on whether "x" is to be read or written.
- If "x" is to be read, it follows the famous "LEGB" ( not LGBG:) ) rule.
- If "x" is to be written, only local variables are searched.
E.g.
x = 0
def f1():
print(x) # to read x, x is searched in all scopes
f1()
In code above, within function f1, "x" is to be read, so it's found by LEGB rule.
But if we change our code a lit bit, like below
x = 0
def f1():
print(x) # x is searched in local scope
x = 99
f1()
Python will pop out an error, "UnboundLocalError: x". The reason is in f1, "x" is treated as to be written now, so only local scope is searched. "print(x)" cannot find a variable named "x" in local function scope.
1.2 variable scope can be explicitly denoted
For "x=99", the later rule is used. So if the context is
x = 0
x = 99
x=99 is not to create a new variable.
Then how to write to a non local variable in Python in a function, instead of creating a new local one? The solution is to explicitly set a variable's scope with keyword "global" or "nonlocal".
e.g.
x = 0
def f1():
global x
x = 20
f1()
print(x)
Now "x" will be searched in global scope even if it is to be written. So the output will be 20.
1.3 Problems caused
It's common for Python beginners to forget adding "global" or "nonlocal" as Python still happily run your code without any errors. But usually the result is not what you want. e.g.
x = 0 // this is a global "x"
def f1():
x = 20 // this will create a new local variable "x"
f1()
print(x)
2 JavaScript is simpler ?
2.1 JS doesn't differentiate variable reading and writing
Unlike Python, JavaScript resolve a variable name in the same way for reading and writing.
x = 0
function myf(){
x = 2 // x is searched in all scopes
}
myf();
console.log(x);
The output will be "2".
Then, how to create a local variable if a same named global variable exists?
2.2 JS also supports explicitly scope denotion
Just like Python, JS allows to expliciltly set a variable's scope.
x = 0
function myf(){
var x = 2 // x is only searched in local scope now
}
myf();
console.log(x);
By adding "var" before "x", "x" is now a local variable to create.
The output will be "0" this time.
2.3 Problems caused
Just the opposite to Python, JS beginners often forget to add "var" and JS doesn't give errors.
3 Golang learned the lesson
To avoid problems in Python and JS, Golang seperates declaration and assignment by inventing the warlus operator.
x := 10 // this is variable delcaration
x = 10 // this is variable assignment
I would say it works!
No comments:
Post a Comment