การใช้ decorator @property
หลังจากที่ได้ฝึกทำงานกับคลาสที่ใช้ descriptor แล้ว ตอนนี้จะได้ลองสร้าง descriptor ของตัวเองโดยใช้ decorator @property กัน โชคดี!
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Object-Oriented Programming ใน Python ระดับกลาง
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
class BankAccount:
def __init__(self, email):
self.email = email
# Define and decorate a method to begin the process of
# creating a descriptor for the email attribute
@____
def ____(self):
return f"Email for this account is: {____.____}"