This repository was archived by the owner on Dec 1, 2022. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproperty.py
More file actions
Latest commit
42 lines (34 loc) · 1.18 KB
/
Copy pathproperty.py
File metadata and controls
42 lines (34 loc) · 1.18 KB
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
34
35
36
37
38
39
40
41
42
# property 사용합니다.
classCar(object):
def__init__(self, model=None):
self.model=model
defrun(self):
print('run')
classHyundaiCar(Car):
defrun(self):
print('fast')
classTeslaCar(Car):
def__init__(self, model='Model S', enable_auto_run=False, passwd='123'):
super().__init__(model)
# enable 앞에 _를 붙이며 property를 사용한다는 것을 알린다.
self._enable_auto_run=enable_auto_run
self.passwd=passwd
# property 함수를 선언해서 값을 임의로 바꿀 수 없게 한다.
@property
defenable_auto_run(self):
returnself._enable_auto_run
# setter 넣으면 임의로 변경이 가능하다.(비밀번호 확인의 용도로도 가능)
@enable_auto_run.setter
defenable_auto_run(self, is_enable):
ifself.passwd=='456':
self._enable_auto_run=is_enable
else:
raiseValueError
defrun(self):
print('super fast')
defauto_run(self):
print('auto run')
# 패스워드를 설정
tesla_car=TeslaCar('Model S', passwd='456')
tesla_car.enable_auto_run=True
print(tesla_car.enable_auto_run)