ในบทความนี้ เราจะเรียนรู้เกี่ยวกับวิธีแก้ปัญหาตามที่ระบุด้านล่าง
คำชี้แจงปัญหา − เราได้รับฟังก์ชัน เราต้องแสดงจำนวนตัวแปรท้องถิ่นในฟังก์ชัน
ทีนี้มาดูวิธีแก้ปัญหาในการใช้งานด้านล่าง -
ตัวอย่าง
# checking locals
def scope():
a = 25.5
b = 5
str_ = 'Tutorialspoint'
# main
print("Number of local varibales available:",
scope.__code__.co_nlocals) ผลลัพธ์
Number of local varibales available: 3
ตัวอย่าง
# checking locals
def empty():
pass
def scope():
a, b, c = 9, 23.4, True
str = 'Tutiorialspoint'
# main
print("Number of local varibales
available:",empty.__code__.co_nlocals)
print("Number of local varibales
available:",scope.__code__.co_nlocals) ผลลัพธ์
Number of local varibales available: 0 Number of local varibales available: 4
บทสรุป
ในบทความนี้ เราได้เรียนรู้เกี่ยวกับวิธีการสร้างโปรแกรม Python เพื่อค้นหาตัวแปรท้องถิ่นจำนวนหนึ่งในฟังก์ชัน