无意中发现一个几篇很nice的Python英文推文,思路、结构都清晰。在此,结合自己的理解,稍作翻译与内容讲解,虽然都是英文的,但是木有想象中的难的,小伙子加油哈!
本节知识点:(1)Python中global和local variables;(2)初识global关键字
Global variables
Global variables are the one that are defined and declared outside a function and we need to use them inside a function.
# This function uses global variable s
def f():
print(s)
# Global scope
s = "I love Geeksforgeeks"
f()
I love Geeksforgeeks
The variable s is defined as the string “I love Geeksforgeeks” before we call the function f(). The only statement in f() is the “print s” statement. As there is no local s, the value from the global s will be used.
notes: Global variables 定义在function以外
Local Variables
# This function