โครงสร้างของคลาส: constructor __init__

การเขียนโปรแกรมเชิงวัตถุใน Python

Alex Yarosh

Content Quality Analyst @ DataCamp

เมธอดและ attribute

  • เมธอดคือฟังก์ชันที่นิยามภายในคลาส
  • self เป็น argument ตัวแรก
  • สร้าง attribute ด้วยการกำหนดค่า
  • อ้างอิง attribute ในคลาสผ่าน self.___

   

class MyClass:
    # function definition in class
    # first argument is self
    def my_method1(self, other_args...):
        # do things here

    def my_method2(self, my_attr):        
        # attribute created by assignment
        self.my_attr = my_attr
        ...
การเขียนโปรแกรมเชิงวัตถุใน Python

Constructor

  • ต้องการเพิ่มข้อมูลให้ออบเจกต์ตอนสร้างใช่ไหม?
  • เมธอด constructor __init__() ถูกเรียกทุกครั้งที่สร้างออบเจกต์
class Customer:
       def __init__(self, name):     
        self.name = name           # <--- Create the .name attribute and set it to name parameter
        print("The __init__ method was called")

cust = Customer("Lara de Silva") #<--- __init__ is implicitly called print(cust.name)
The __init__ method was called
Lara de Silva
การเขียนโปรแกรมเชิงวัตถุใน Python
class Customer:
       def __init__(self, name, balance):  # <-- balance parameter added

self.name = name self.balance = balance # <-- balance attribute added print("The __init__ method was called")
cust = Customer("Lara de Silva", 1000) # <-- __init__ is called print(cust.name) print(cust.balance)
The __init__ method was called
Lara de Silva
1000
การเขียนโปรแกรมเชิงวัตถุใน Python
class Customer:
       def __init__(self, name, balance=0):  #<--set default value for balance

self.name = name self.balance = balance print("The __init__ method was called")
cust = Customer("Lara de Silva") # <-- don't specify balance explicitly
print(cust.name) print(cust.balance) # <-- attribute is created anyway
The __init__ method was called
Lara de Silva
0
การเขียนโปรแกรมเชิงวัตถุใน Python

Attribute ในเมธอด

class MyClass:
    def my_method1(self, attr1):
        self.attr1 = attr1
        ...

    def my_method2(self, attr2):        
        self.attr2 = attr2
        ...
obj = MyClass()
obj.my_method1(val1) # <-- attr1 created
obj.my_method2(val2) # <-- attr2 created

Attribute ใน constructor

class MyClass:
    def __init__(self, attr1, attr2):
        self.attr1 = attr1
        self.attr2 = attr2
        ...

# All attributes are created obj = MyClass(val1, val2)
  • รู้ว่ามี attribute อะไรบ้างได้ง่ายกว่า
  • attribute ถูกสร้างพร้อมกับออบเจกต์
  • โค้ดใช้งานและบำรุงรักษาได้ง่ายขึ้น
การเขียนโปรแกรมเชิงวัตถุใน Python

แนวปฏิบัติที่ดี

1. กำหนดค่าเริ่มต้น attribute ใน __init__()
การเขียนโปรแกรมเชิงวัตถุใน Python

แนวปฏิบัติที่ดี

1. กำหนดค่าเริ่มต้น attribute ใน __init__()
2. การตั้งชื่อ

CamelCase สำหรับคลาส, lower_snake_case สำหรับฟังก์ชันและ attribute

การเขียนโปรแกรมเชิงวัตถุใน Python

แนวปฏิบัติที่ดี

1. กำหนดค่าเริ่มต้น attribute ใน __init__()
2. การตั้งชื่อ

CamelCase สำหรับคลาส, lower_snake_case สำหรับฟังก์ชันและ attribute

3. ใช้ self เป็น self เสมอ
class MyClass:
    # This works but isn't recommended
    def my_method(kitty, attr):
       kitty.attr = attr
การเขียนโปรแกรมเชิงวัตถุใน Python

แนวปฏิบัติที่ดี

1. กำหนดค่าเริ่มต้น attribute ใน __init__()
2. การตั้งชื่อ

CamelCase สำหรับคลาส, lower_snake_case สำหรับฟังก์ชันและ attribute

3. self คือ self
4. ใช้ docstring
class MyClass:
    """This class does nothing"""
    pass
การเขียนโปรแกรมเชิงวัตถุใน Python

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

การเขียนโปรแกรมเชิงวัตถุใน Python

Preparing Video For Download...