การเขียนเอกสารให้กับคลาสใหม่

กรณีศึกษา: การสร้างซอฟต์แวร์ด้วย Python

Mark Pedigo, PhD

Principal Data Scientist

แผนที่

แผนที่แสดงส่วนที่ดำเนินการเสร็จแล้วและส่วนที่กำลังจะเริ่มต้น

กรณีศึกษา: การสร้างซอฟต์แวร์ด้วย Python

ลำดับชั้นของคลาส

  • BasicCalculator
    • คำนวณเลขคณิตพื้นฐาน
  • FinancialCalculator
    • คลาสลูกของ BasicCalculator
    • เพิ่มฟีเจอร์ทางการเงิน เช่น อัตราดอกเบี้ย
  • MortgageCalculator
    • สืบทอดจาก FinancialCalculator
    • รวมฟังก์ชันของทั้งสองคลาส
กรณีศึกษา: การสร้างซอฟต์แวร์ด้วย Python

Docstrings

  • ประโยชน์ของ docstring
    • อธิบายโค้ดได้ชัดเจนและกระชับ
  • วิธีสร้าง docstring
    • เพิ่ม string literal เป็นคำสั่งแรก
    • เขียนคำอธิบายวัตถุประสงค์และการทำงาน
def MortgageClass(FinancialCalculator):
    """
    MortgageClass extends the FinancialCalculator to mortgage specific calculations.
    """
    ...
กรณีศึกษา: การสร้างซอฟต์แวร์ด้วย Python

เพิ่ม attribute ให้กับคลาส MortgageCalculator

    class MortgageCalculator(FinancialCalculator):
    def __init__(self, loan_amount, annual_interest_rate, years):
        super().__init__()
        self.loan_amount = loan_amount
        self.monthly_interest_rate = self.monthly_interest(annual_interest_rate)
    self.months = years * 12
    # Create an attribute to hold the monthly payment amount.
    self.monthly_payment = self.calculate_monthly_payment()
กรณีศึกษา: การสร้างซอฟต์แวร์ด้วย Python

ติดตามฟังก์ชันของคลาส

  • ใช้ dir(ClassName) เพื่อดูรายการฟังก์ชันของคลาส
dir(MortgageCalculator)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', 
'__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', 
'__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', 
'__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
'__sizeof__', '__str__', '__subclasshook__', '__weakref__', 
'difference', 'monthly_interest', 'months_from_years',
'power', 'product', 'quotient', 'sum']
กรณีศึกษา: การสร้างซอฟต์แวร์ด้วย Python

มาฝึกกันเถอะ!

กรณีศึกษา: การสร้างซอฟต์แวร์ด้วย Python

Preparing Video For Download...