#100DaysOfPyCon2018 Day3: Dataclasses: The code generator to end all code generators

Key takeaways

ความสามารถของ Dataclass

Customize Fields Specification

นอกจากจะกำหนดพฤติกรรมของ dataclass ได้จาก decorator แล้วเรายังสามารถใช้ฟังก์ชั่น field() ในการกำหนดพฤติกรรมของแต่ละ attribute ได้ด้วย ตัวอย่างข้างบนเป็น use case คล้ายๆ ของจริงที่ต้องการสร้าง data structure ของ employee ขึ้นมา แล้วเก็บข้อมูลต่างๆ ในนี้ใช้ความสามารถของ field() หลากหลายมาก

จริงๆ ในช่วงท้ายมีพูดถึง slots ด้วยแต่ผมฟังไม่ค่อยเข้าใจ รู้แค่ว่ามันจะ optimize ให้เรา access ของได้เร็วขึ้น กับเรื่อง __post_init__() ที่ไม่สามารถใช้ได้ อะไรประมาณนี้

วันก่อนผมไปเจอ blog ของ RealPython เขียนเรื่อง dataclass ไว้พอดี อธิบายไว้ดีพอสมควรเลยครับ รวมถึงเรื่อง slots ด้วยสนใจก็ไปอ่านต่อกันได้

--

--

Pythonista @ProntoTools ♥ Python, Django, Vim and Star Trek 🖖

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store