-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathexample_004_01_oop_encapsulation.py
49 lines (39 loc) · 1.24 KB
/
example_004_01_oop_encapsulation.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""An example of encapsulation."""
class Car():
"""A simple Car class to demonstrate encapsulation."""
def __init__(self, year=1885):
self.year = year
# “Private” instance variables that cannot be accessed except from
# inside an object don’t exist in Python.
@property
def year(self):
"""Method to return year value."""
print("Get year value...")
return self.__year
@year.setter
def year(self, year):
"""Method to set year value."""
print("Set year value...")
if 1885 < year < 2022:
self.__year = year
else:
raise ValueError("Year must be between 1885 and 2022")
# self.__year = 0
# print("Year must be between 1885 and 2022")
def __str__(self):
return str(self.year)
m_car1 = Car(2021)
print(m_car1)
# print(dir(m_car1))
print(m_car1.year)
m_car1.year = 2019
print(m_car1.year)
# 'Car' object has no attribute '__year'
# print(m_car1.__year)
# AttributeError: 'Car' object has no attribute '__year'
print("###### Check if we can create a car with build year 1050 #######")
try:
m_car2 = Car(1050)
print(m_car2)
except ValueError as e:
print("Can not create this car.", e)