ในการประกาศฟังก์ชัน รายการ Parameter คือ การระบุว่า ฟังก์ชันนั้นจะรับ Argument อะไรบ้าง

ในการเขียนโปรแกรม เราอาจจะไม่ได้คิดครอบคลุมในทุกเคสที่เป็นไปได้ทั้งหมด ในบางครั้งเราอาจจะอยากเปิดช่องไว้สำหรับ Argument ที่จะถูกใช้ในอนาคต เราสามารถทำได้โดยใช้ args, kwargs

*args คืออะไร

*args คือ Parameter ที่ใช้แทน Parameter ตามตำแหน่ง (ไม่มีชื่อ) กี่ตัวก็ได้ มี Type เป็น Tuple

**kwargs คืออะไร

**kwargs คือ Parameter ที่ใช้แทน Parameter แบบมีชื่อ กี่ตัวก็ได้ มี Type เป็น Dictionary

เรามาดูตัวอย่างจะเห็นภาพชัดมากขึ้น

เรามาเริ่มกันเลยดีกว่า

Open In Colab

แชร์ให้เพื่อน:

Surapong Kanoktipsatharporn on FacebookSurapong Kanoktipsatharporn on LinkedinSurapong Kanoktipsatharporn on Rss
Surapong Kanoktipsatharporn
Solutions Architect at Bua Labs
The ultimate test of your knowledge is your capacity to convey it to another.

Published by Surapong Kanoktipsatharporn

The ultimate test of your knowledge is your capacity to convey it to another.