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

Python เบื้องต้นสำหรับการเขียนโปรแกรมเชิงวัตถุ

George Boorman

Curriculum Manager, DataCamp

เมธอดและ attribute

  • เมธอดคือฟังก์ชันที่นิยามภายในคลาส
  • self เป็น argument แรกเสมอ
  • กำหนด attribute ด้วยการ assignment
  • อ้างอิง 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__() ถูกเรียกทุกครั้งที่สร้างออบเจกต์
    • เรียกอัตโนมัติเพราะ syntax __methodname__
class Customer:
    def __init__(self, name):
        # Create the .name attribute and set it to name parameter
        self.name = name
        print("The __init__ method was called")
Python เบื้องต้นสำหรับการเขียนโปรแกรมเชิงวัตถุ

Constructor

# __init__ is implicitly called 
cust = Customer("Lara de Silva")   
print(cust.name)
The __init__ method was called
Lara de Silva
Python เบื้องต้นสำหรับการเขียนโปรแกรมเชิงวัตถุ

Attribute ในเมธอด

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

    def my_method2(self, attr2):        
        self.attr2 = attr2
        ...

obj = MyClass() # attr1 created obj.my_method1(val1) # attr2 created obj.my_method2(val2)

Attribute ใน constructor

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

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

เพิ่ม argument

class Customer:
    # Add balance argument
    def __init__(self, name, balance): 

self.name = name # Add the balance attribute self.balance = balance print("The __init__ method was called")
Python เบื้องต้นสำหรับการเขียนโปรแกรมเชิงวัตถุ

เพิ่ม parameter

# __init__ is called
cust = Customer("Lara de Silva", 1000)
print(cust.name)
print(cust.balance)
The __init__ method was called
Lara de Silva
1000
Python เบื้องต้นสำหรับการเขียนโปรแกรมเชิงวัตถุ

argument แบบมีค่าเริ่มต้น

class Customer:
    # Set a default value for balance
    def __init__(self, name, balance=0):
        self.name = name
        # Assign the new attribute
        self.balance = balance
        print("The __init__ method was called")
Python เบื้องต้นสำหรับการเขียนโปรแกรมเชิงวัตถุ

argument แบบมีค่าเริ่มต้น

# Don't specify the balance explicitly
cust = Customer("Lara de Silva")

print(cust.name) # The balance attribute is created anyway print(cust.balance)
The __init__ method was called
Lara de Silva
0
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(george, attr):
        george.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...