Python的变量作用域
合法
gcount = 0 def global_test(): print (gcount) global_test()
不合法,报错行:
tmp = gcount + 1
gcount = 0 def global_test(): tmp = gcount + 1 gcount = tmp print (gcount) global_test()
不合法
gcount = 0 def global_test(): gcount += 1 print (gcount) global_test()
合法
gcount = [0] def global_test(): gcount[0] += 1 print (gcount) global_test()
ChatGPT
gcount = 0 def global_test(): gcount += 1 print (gcount) global_test()
This code will result in an error because the function tries to modify the global variable gcount without explicitly declaring it as global. Here's why:
Explanation
- In Python, variables inside a function are considered local by default.
- When you write
gcount += 1, Python interprets it as:- Access
gcount(assumes it's a local variable). - Increment it by 1.
- Assign the new value back to
gcount.
- Access
Since gcount is not declared as local or global, Python raises an UnboundLocalError, indicating that you're trying to use a variable before it has been assigned a value locally.
Corrected Code
To modify the global variable gcount inside the function, you need to explicitly declare it as global:
gcount = 0
def global_test():
global gcount # Declare gcount as global
gcount += 1
print(gcount)
global_test()
Output of Corrected Code
1
gcount = [0] def global_test(): gcount[0] += 1 print (gcount) global_test()
This code will work as expected because you are modifying the mutable object gcount (a list in this case) without needing to declare it as global. Here's why:
Explanation
- The variable
gcountholds a reference to a list object[0]. - When you write
gcount[0] += 1, you are modifying the first element of the list. - Since you are not reassigning the
gcountvariable itself but altering the content of the list, theglobalkeyword is not required.
Output
The output of this code will be:
[1]
Key Difference
- In the original code, you were trying to modify an immutable integer (
gcount += 1), which requires reassigning the variable. For that, you need theglobalkeyword. - In the corrected code, you're modifying a mutable object (list), which does not require reassigning
gcount, so noglobaldeclaration is necessary.
gcount = 0 def global_test(): tmp = gcount + 1 gcount = tmp print (gcount) global_test()
This code will raise an error because you are trying to assign a value to gcount inside the function without declaring it as global. Here's a detailed explanation:
Explanation of Error
- When the line
tmp = gcount + 1is executed, Python looks for the value ofgcount.- Since
gcountis a global variable, it is accessible, sotmpgets assigned0 + 1 = 1.
- Since
- On the next line,
gcount = tmp, Python interpretsgcountas a local variable because it is being assigned a value.- This means
gcountis considered local to the functionglobal_test. - However, the previous line (
tmp = gcount + 1) tried to usegcountbefore it was defined locally.
- This means
- This results in an
UnboundLocalErrorbecause Python doesn't know about the globalgcountunless you explicitly declare it asglobal.
Corrected Code
To fix this, you need to declare gcount as global to indicate that the function should use the global gcount:
gcount = 0
def global_test():
global gcount # Declare gcount as global
tmp = gcount + 1
gcount = tmp
print(gcount)
global_test()
Output of Corrected Code
1
Alternatively, if you do not want to use the global keyword, you could work with a mutable object (e.g., a list):
gcount = [0]
def global_test():
gcount[0] += 1 # Modify the first element of the list
print(gcount[0])
global_test()
Output for List-Based Code
1

浙公网安备 33010602011771号