Partial Function คือ การสร้างฟังก์ชันใหม่ ขึ้นมาจากฟังก์ชันที่มีอยู่ โดย Fix Parameter ส่วนหนึ่งไว้ ทำให้เราได้ฟังก์ชันใหม่ที่ต้องการ Parameter น้อยลง ทำให้ Signature ของฟังก์ชันดูเรียบง่ายขึ้นกว่าเดิม

ตัวอย่างการใช้งาน functools.partial

ในการเรียกใช้ API บางตัว เราจะต้องพาส Function ที่มีจำนวน Parameter ตามที่ API กำหนด ถ้าฟังก์ชันของเราต้องการ Parameter มากเกินกว่านั้น เราสามารถใช้ Partial Function มาแก้ปัญหานี้ โดยสร้างฟังก์ชันใหม่ ขึ้นมาจากฟังก์ชันเดิม แต่มี Parameter น้อยลงเป็นตามที่ API กำหนด

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

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.