ทำความรู้จักกับภาษา python (24) : Namespace (เนมสเปช) and Scoping (สคอปปิ้ง)

แชร์ความรู้ภาษา Python ไพทอน การเขียนโปรแกรมภาษาไพทอน

Moderator: mindphp, ผู้ดูแลกระดาน

ภาพประจำตัวสมาชิก
chatee supasand
PHP VIP Members
PHP VIP Members
โพสต์: 1666
ลงทะเบียนเมื่อ: 04/06/2019 10:06 am

Re: ทำความรู้จักกับภาษา python (24) : Namespace (เนมสเปช) and Scoping (สคอปปิ้ง)

โพสต์ที่ยังไม่ได้อ่าน โดย chatee supasand »

ทดลองทำตามดูแล้วเข้าใจมากๆเลยครับ
รูปภาพ
ภาพประจำตัวสมาชิก
jirawoot
PHP VIP Members
PHP VIP Members
โพสต์: 3129
ลงทะเบียนเมื่อ: 17/06/2019 10:30 am

Re: ทำความรู้จักกับภาษา python (24) : Namespace (เนมสเปช) and Scoping (สคอปปิ้ง)

โพสต์ที่ยังไม่ได้อ่าน โดย jirawoot »

global namespce

โค้ด: เลือกทั้งหมด

var_int = 10
def addint():
    global  var_int
    var_int= var_int+1

print(var_int)
addint()
print(var_int)

ผลลัพธ์
Selection_006.png
Selection_006.png (6.07 KiB) Viewed 601 times
dir()

โค้ด: เลือกทั้งหมด

from global_name import *
content = dir(addint)
print(content)
ผลลัพธ์
Selection_007.png
Selection_007.png (9.08 KiB) Viewed 601 times
global() locals()

โค้ด: เลือกทั้งหมด

var_int = 10
def addint():
    global  var_int
    var_inside ='local variable'
    g = globals()
    print(g)
    l = locals()
    print(l)

    var_int= var_int+1


addint()

ผลลัพธ์
Selection_008.png
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 41