ทำความรู้จักกับภาษา python (21) : ขอบเขตของตัวแปรฟังก์ชัน

ตอบกระทู้

รูปแสดงอารมณ์
:icon_plusone: :like: :plusone: :gfb: :-D :) :( :-o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: :angry: :baa: :biggrin:
รูปแสดงอารมณ์อื่นๆ

BBCode เปิด
[img] เปิด
[url] เปิด
[Smile icon] เปิด

กระทู้แนะนำ
   

มุมมองที่ขยายได้ กระทู้แนะนำ: ทำความรู้จักกับภาษา python (21) : ขอบเขตของตัวแปรฟังก์ชัน

Re: ทำความรู้จักกับภาษา python (21) : ขอบเขตของตัวแปรฟังก์ชัน

โดย jirawoot » 20/06/2019 6:19 pm

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

total = 0
def mysum(arg1, arg2, arg3):
    total = arg1+arg2+arg3
    print("ตัวแปรที่เป็น local=", total)
    return total

mysum(10,20,30)
print("ตัวแปรที่เป็น global=", total)
ผลลัพธ์
Selection_025.png
Selection_025.png (7.91 KiB) Viewed 352 times

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

total = 0
def mysum(arg1, arg2, arg3):
    total = arg1+arg2+arg3
    print("ตัวแปรที่เป็น local=", total)
    return total

total=30
mysum(10,20,30)
print("ตัวแปรที่เป็น global=", total)
ผลลัพธ์
Selection_026.png
Selection_026.png (7.17 KiB) Viewed 352 times

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

def myfunc(a,b):
    a=10
    print('ตัวแปร a =',a)
    print('ตัวแปร b =', b)

a=20
b=30
print('global a=',a)
print('global b=',b)
print('----------------')
myfunc(a,b)
ผลลัพธ์
Selection_027.png
Selection_027.png (7.8 KiB) Viewed 352 times

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

def myfunc2(c, d):
    print('เรียกใช้ฟังก์ชั้นที่ 2')
    return (c+d)

a=20
b=30
print('global a =',a)
print('globla b =',b)
print('----------------')
total= myfunc2(a, b)
print('local a+b=', total)
ผลลัพธ์
Selection_028.png
Selection_028.png (10.18 KiB) Viewed 352 times

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

def myfunc3():
    a = 0
    b = 1
    print('เรียกฟังก์ชั่นที่ 3')
    print("local a=",a)
    print('local b=', b)

a= 20
b=30
print('global a=',a)
print('globle b=',b)
print('---------------')
myfunc3()
ผลลัพธ์
Selection_029.png
Selection_029.png (8.71 KiB) Viewed 352 times

Re: ทำความรู้จักกับภาษา python (21) : ขอบเขตของตัวแปรฟังก์ชัน

โดย chatee supasand » 10/06/2019 6:17 pm

สอนดีเวรี่กู้ดมากครับ

Re: ทำความรู้จักกับภาษา python (21) : ขอบเขตของตัวแปรฟังก์ชัน

โดย ธวัชชัย แสนหาญ » 16/11/2018 6:02 pm

การใช้ตัวแปรชนิด tuple ใน python
Tuple เป็นตัวแปรชนิดหนึ่งที่สามารถเก็บค่าได้หลายค่าใน1 ตัวแปร
เหมือนกับ list แต่ไม่สามารถ เพิ่ม และ ลบค่าได้
แต่ละค่าหรือสามชิกแต่ละตัวจะต้องอยู่ใน (...)

รูปที่ run ในโปรแกรม
51.png
51.png (29.49 KiB) Viewed 1167 times
โค้ดที่จะนำมาrun

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

Tuple1 =('in','out',1,2)
Tuple2 =(10,20,30,40)

print (Tuple1)        #แสดงคาใน Tuple1 ทั้งหมด
print (Tuple1[1])     #แสดงคาใน Tuple1 ใน index ที่1
print (Tuple2)        #แสดงคาใน Tuple2 ทั้งหมด
print (Tuple2[-1])    #แสดงคาใน Tuple2 -1คือ นับจากข้างหลังลงมา 1 ค่า
ผลลัพธ์ที่ได้
51.png
51.png (29.49 KiB) Viewed 1167 times
แนบไฟล์
52.png
52.png (12.81 KiB) Viewed 1167 times

Re: ทำความรู้จักกับภาษา python (21) : ขอบเขตของตัวแปรฟังก์ชัน

โดย anuwat somsakul » 10/08/2018 3:57 pm

ขอบเขตของตัวแปล ใน การสร้างฟังก์ชั่น Scope of Variables

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

total = 0
def mysum(arg1,arg2,arg3):
    total = arg1 + arg2 + arg3 #ตัวแปร local
    print("แสดงค่าของตัวแปรที่เป็นlocal",total)
    return  total

#ชื่อตัวแปรglobalซ้ำ
total = 30
mysum(10,20,30)
print("แสดงค่าของตัวแปรที่เป็นglobal",total)
ผลลัพธ์
Selection_013.png
Selection_013.png (12.58 KiB) Viewed 1217 times
ศึกษาจาก : https://www.youtube.com/watch?v=9KP4C9g ... lzdKrpxsMM

Re: ทำความรู้จักกับภาษา python (21) : ขอบเขตของตัวแปรฟังก์ชัน

โดย prakon » 06/07/2018 3:52 pm

ตัวแปร global ในฟังก์ชั่นจะทำให้ ตัวแปรนั้น ค่าเดียวกันทั้งในและนอกฟังก์ชั่น
แต่ตัวแปร local ใช้ได้เพียงในฟังก์ชั่น

โค้ดตัวอย่างกรณี ตัวแปร global

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

import time
a=0
def count():
    global a
    a+=1
    print(a)
while 1:
    count()
    time.sleep(1)
จะเห็นได้ว่าตัวแปร

Re: ทำความรู้จักกับภาษา python (21) : ขอบเขตของตัวแปรฟังก์ชัน

โดย rangsan » 03/05/2018 1:55 pm

ขอบเขตของตัวแปรฟังก์ชัน

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

total = 0
def mySum(arg1,arg2,arg3):
    total1 = arg1+arg2+arg3
    total2 = arg1*arg2-arg3
    total = total1+total2
    print 'Value Total in Local  : ',total
    return total

total = 20
mySum(2,3,4)
print '--------------------------'
print 'Value Total in Global : ',total
ผลลัพธ์การรันโค้ด
ขอบเขตของตัวแปรฟังก์ชัน.png
ขอบเขตของตัวแปรฟังก์ชัน.png (17.62 KiB) Viewed 1928 times
จะเห้นได้ว่าตัวแปรของ total ในแบบ Local และ total ในแบบ Global นั้นมีค่าที่ต่างกันถึงแม้ว่าจะมีการ return ค่าออกมาแต่เราไม่ได้เอาค่านั้นมาทำงานต่อแต่อย่างใดค่า total ในแบบ Global จึงมีค่าเท่ากับ 20 เพราะมีการประกาศค่าซ้ำอีกครั้ง

ศึกษาจาก : https://www.youtube.com/watch?v=9KP4C9g3ys8

Re: ทำความรู้จักกับภาษา python (21) : ขอบเขตของตัวแปรฟังก์ชัน

โดย Patcharanan.0399 » 21/04/2018 11:30 am

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

total = 0
def mysum(arg1, arg2, arg3):
    total = arg1 + arg2 + arg3
    print 'Local is : ', total
    return total

mysum(15, 25, 35)
print 'Globle is : ', total
ผลการรัน
ตัวแปรมีค่าไม่เท่ากัน แม้ว่าจะเป็นชื่อเดียวกัน เพราะเรื่องขอบเขตของตัวแปร Global กับ Local
ตัวแปรมีค่าไม่เท่ากัน แม้ว่าจะเป็นชื่อเดียวกัน เพราะเรื่องขอบเขตของตัวแปร Global กับ Local
Global.JPG (10.65 KiB) Viewed 1950 times

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

total = 0
def mysum(arg1, arg2, arg3):
    total = arg1 + arg2 + arg3
    print 'Local is : ', total
    return total

total = 30
mysum(15, 25, 35)
print 'Globle is : ', total
ผลการรัน
เมื่อมีชื่อตัวแปร Global ซ้ำ จะเท่ากับค่าที่กำหนดไว้ล่าสุด
เมื่อมีชื่อตัวแปร Global ซ้ำ จะเท่ากับค่าที่กำหนดไว้ล่าสุด
Global1.JPG (11.08 KiB) Viewed 1950 times

ศึกษาจาก https://youtu.be/9KP4C9g3ys8

Re: ทำความรู้จักกับภาษา python (21) : ขอบเขตของตัวแปรฟังก์ชัน

โดย Jom07 » 25/01/2018 1:13 am

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

total = 0
def test(a,b,c):
    total = a + b + c
    print('แสดงผลรัน local = ', total)
    return total

total = 30
test(10,20,30)
print('แสดงผล global =',total)
ผลรัน

รูปภาพ

Re: ทำความรู้จักกับภาษา python (21) : ขอบเขตของตัวแปรฟังก์ชัน

โดย Four » 23/01/2018 4:32 pm

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

def func():
    a = 1
    b = 2
    print("local A", a)
    print("local B", b)

a = 10
b = 20
print("local A", a)
print("local B", b)
func()
print("local A", a)
print("local B", b)
ผลรัน

รูปภาพ

Re: ทำความรู้จักกับภาษา python (21) : ขอบเขตของตัวแปรฟังก์ชัน

โดย Dive Demo » 03/02/2017 5:11 pm

ในหัวข้อนี้คือต้องตระหนักถึงตำแหน่งของ ตัวแปร Global และ Local เป็นสำคัญเพราะมีผลในการประมวลผลของระบบ

ข้างบน