Properties

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

Alex Yarosh

Content Quality Analyst @ DataCamp

การเปลี่ยนค่า attribute

class Employee:
  def set_name(self, name):
    self.name = name 
  def set_salary(self, salary):
    self.salary = salary 
  def give_raise(self, amount):
    self.salary = self.salary + amount

def __init__(self, name, salary): self.name, self.salary = name, salary
emp = Employee("Miriam Azari", 35000)
# Use dot syntax and = to alter atributes
emp.salary = emp.salary + 5000
การเขียนโปรแกรมเชิงวัตถุใน Python

การเปลี่ยนค่า attribute

class Employee:
  def set_name(self, name):
    self.name = name 
  def set_salary(self, salary):
    self.salary = salary 
  def give_raise(self, amount):
    self.salary = self.salary + amount

def __init__(self, name, salary): self.name, self.salary = name, salary
emp = Employee("Miriam Azari", 35000)
# Use dot syntax and = to alter atributes
emp.salary = emp.salary + 5000

 

 

ควบคุมการเข้าถึง attribute?

  • ตรวจสอบความถูกต้องของค่า
  • หรือทำให้ attribute เป็น read-only
    • การแก้ไข set_salary() ไม่ได้ป้องกัน emp.salary = -100
การเขียนโปรแกรมเชิงวัตถุใน Python

Attribute แบบจำกัดและ read-only

import pandas as pd
df = pd.DataFrame({"colA": [1,2], "colB":[3,4]})
df
  colA colB
0    1    3
1    2    4
df.columns = ["new_colA", "new_colB"]
df
  new_colA  new_colB
0    1    3
1    2    4
# will cause an error
df.columns = ["new_colA", "new_colB", "extra"]
df
ValueError: Length mismatch: 
Expected axis has 2 elements, 
new values have 3 elements
df.shape = (43, 27)
df
AttributeError: can't set attribute
การเขียนโปรแกรมเชิงวัตถุใน Python

@property

class Employer:
  def __init__(self, name, new_salary):
     self._salary = new_salary


@property def salary(self): return self._salary
@salary.setter def salary(self, new_salary):
if new_salary < 0: raise ValueError("Invalid salary") self._salary = new_salary

 

← ใช้ attribute แบบ "protected" ที่มี _ นำหน้าเพื่อเก็บข้อมูล

← ใช้ @property กับ เมธอด ที่มีชื่อตรงกับ attribute ที่ต้องการจำกัด แล้ว return attribute ภายใน

← ใช้ @attr.setter กับเมธอด attr() ที่จะถูกเรียกเมื่อ obj.attr = value

  • ค่าที่ต้องการกำหนดจะถูกส่งเป็น argument
การเขียนโปรแกรมเชิงวัตถุใน Python

@property

class Employer:
  def __init__(self, name, new_salary):
     self._salary = new_salary


@property def salary(self): return self._salary
@salary.setter def salary(self, new_salary): if new_salary < 0: raise ValueError("Invalid salary") self._salary = new_salary
emp = Employee("Miriam Azari", 35000)
# accessing the "property"
emp.salary
35000
emp.salary = 60000 # <-- @salary.setter
emp.salary = -1000
ValueError: Invalid salary
การเขียนโปรแกรมเชิงวัตถุใน Python

ทำไมต้องใช้ @property?

  $$\text{\Large{User-facing: behave like attributes}}$$

 

$$\text{\Large{Developer-facing: give control of access}}$$

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

ความเป็นไปได้อื่น ๆ

→ ไม่ต้องเพิ่ม @attr.setter

สร้าง property แบบ read-only

→ เพิ่ม @attr.getter

ใช้กับเมธอดที่ถูกเรียกเมื่อดึงค่าของ property

→ เพิ่ม @attr.deleter

ใช้กับเมธอดที่ถูกเรียกเมื่อลบ property ด้วย del

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

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

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

Preparing Video For Download...