def func_print_x():
## x += 1 ## if uncomment this line, it will raise UnboundLocalError: local variable 'x' referenced before assignment
print x
if __name__ = '__main__':
x = 4
func_print_x()
在函数 func_print_x()中,有两条规则:
打印功能是否有更多'特权'?
答案 0 :(得分:1)
def f():
global s
print s
s = "That's clear."
print s
s = "Python is great!"
f()
print s
o / p
Python is great!
That's clear.
That's clear.
但是你没有global
def f():
print s
s = "Me too."
print s
s = "I hate spam."
f()
print s
o / p
UnboundLocalError: local variable 's' referenced before assignment
如果您尝试将某些值分配给s
如果您尝试打印s
的值,它将打印在函数