■ 클래스에서 __get__/__set__ 메소드를 사용해 속성을 처리하는 방법을 보여준다.
▶ 예제 코드 (PY)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
class Field: def __init__(self, name): self.name = name self.internalName = f"_{self.name}" def __get__(self, instance, instanceType): if instance is None: return self return getattr(instance, self.internalName, "") def __set__(self, instance, value): setattr(instance, self.internalName, value) class Customer: firstName = Field("firstName") lastName = Field("lastName" ) prefix = Field("prefix" ) sufix = Field("sufix" ) customer = Customer() print("작업전 : ", customer.firstName, customer.__dict__) customer.firstName = "길동" print("작업후 : ", customer.firstName, customer.__dict__) """ 작업전 : '' {} 작업후 : '길동' {'_firstName': '길동'} """ |