Python g lobalคำหลัก
ตัวอย่าง
ประกาศตัวแปรส่วนกลางภายในฟังก์ชัน และใช้ภายนอกฟังก์ชัน:
#create a function:
def myfunction():
global x
x =
"hello"
#execute the function:
myfunction()
#x should now
be global, and accessible in the global scope.
print(x)
ความหมายและการใช้งาน
คีย์เวิร์ด นี้global
ใช้เพื่อสร้างตัวแปรโกลบอลจากขอบเขตที่ไม่มีโกลบอล เช่น ภายในฟังก์ชัน