-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02-private-person-data.py
More file actions
287 lines (200 loc) · 7.23 KB
/
02-private-person-data.py
File metadata and controls
287 lines (200 loc) · 7.23 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
"""Question: Define a class Person with private attributes _name and _age.
Use properties to get and set these attributes with validation
(e.g., age must be a positive integer).
"""
# LEARNING CHALLENGE
#
# Before looking at any solution below, please try to solve this yourself first!
#
# Tips for success:
# - Read the question carefully
# - Think about what classes and methods you need
# - Start with a simple implementation
# - Test your code step by step
# - Don't worry if it's not perfect - learning is a process!
#
# Remember: The best way to learn programming is by doing, not by reading solutions!
#
# Take your time, experiment, and enjoy the learning process!
# Try to implement your solution here:
# (Write your code below this line)
# HINT SECTION (Only look if you're really stuck!)
#
# Think about:
# - What are private attributes? (attributes starting with _)
# - What are properties? (@property decorator)
# - How do you create getters and setters? (@property and @attribute.setter)
# - What validation should you add? (age > 0, name not empty)
#
# Remember: Start simple and build up complexity gradually!
# ===============================================================================
# STEP-BY-STEP SOLUTION
# ===============================================================================
#
# CLASSROOM-STYLE WALKTHROUGH
#
# Let's solve this problem step by step, just like in a programming class!
# Each step builds upon the previous one, so you can follow along and understand
# the complete thought process.
#
# ===============================================================================
# Step 1: Define the Person class with private attributes
# ===============================================================================
# Explanation:
# Let's start by creating our Person class with private attributes.
# In Python, attributes starting with _ are considered private by convention.
class Person:
def __init__(self, name, age):
self._name = name
self._age = age
# What we accomplished in this step:
# - Created Person class with private attributes _name and _age
# Step 2: Add property getter for name
# ===============================================================================
# Explanation:
# Properties allow us to access private attributes through methods that look like attributes.
# The @property decorator creates a getter method.
class Person:
def __init__(self, name, age):
self._name = name
self._age = age
@property
def name(self):
return self._name
# What we accomplished in this step:
# - Added property getter for name attribute
# Step 3: Add property setter for name with validation
# ===============================================================================
# Explanation:
# The @name.setter decorator creates a setter method that includes validation.
# We'll check that the name is not empty.
class Person:
def __init__(self, name, age):
self._name = name
self._age = age
@property
def name(self):
return self._name
@name.setter
def name(self, value):
if not value or not value.strip():
raise ValueError("Name cannot be empty")
self._name = value
# What we accomplished in this step:
# - Added property setter for name with validation
# Step 4: Add property getter for age
# ===============================================================================
# Explanation:
# Now let's add the property getter for age, similar to what we did for name.
class Person:
def __init__(self, name, age):
self._name = name
self._age = age
@property
def name(self):
return self._name
@name.setter
def name(self, value):
if not value or not value.strip():
raise ValueError("Name cannot be empty")
self._name = value
@property
def age(self):
return self._age
# What we accomplished in this step:
# - Added property getter for age attribute
# Step 5: Add property setter for age with validation
# ===============================================================================
# Explanation:
# The age setter should validate that the age is a positive integer.
# We'll check both the type and the value.
class Person:
def __init__(self, name, age):
self._name = name
self._age = age
@property
def name(self):
return self._name
@name.setter
def name(self, value):
if not value or not value.strip():
raise ValueError("Name cannot be empty")
self._name = value
@property
def age(self):
return self._age
@age.setter
def age(self, value):
if not isinstance(value, int) or value <= 0:
raise ValueError("Age must be a positive integer")
self._age = value
# What we accomplished in this step:
# - Added property setter for age with validation
# Step 6: Create instances and test our properties
# ===============================================================================
# Explanation:
# Finally, let's create instances of our Person class and test both the getters
# and setters to make sure validation works correctly.
class Person:
def __init__(self, name, age):
self._name = name
self._age = age
@property
def name(self):
return self._name
@name.setter
def name(self, value):
if not value or not value.strip():
raise ValueError("Name cannot be empty")
self._name = value
@property
def age(self):
return self._age
@age.setter
def age(self, value):
if not isinstance(value, int) or value <= 0:
raise ValueError("Age must be a positive integer")
self._age = value
# Test our class:
person = Person("Alice", 30)
# Test property getters
print(f"Name: {person.name}")
print(f"Age: {person.age}")
# Test property setters with valid values
person.name = "Bob"
person.age = 25
print(f"Updated name: {person.name}")
print(f"Updated age: {person.age}")
# Test validation (these will raise errors if uncommented)
# try:
# person.name = "" # Should raise ValueError
# except ValueError as e:
# print(f"Name validation error: {e}")
# try:
# person.age = -5 # Should raise ValueError
# except ValueError as e:
# print(f"Age validation error: {e}")
print("All property operations completed successfully!")
# What we accomplished in this step:
# - Created and tested our complete Person class with properties
# - Demonstrated both getters and setters with validation
# ===============================================================================
# CONGRATULATIONS!
#
# You've successfully completed the step-by-step solution!
#
# Key concepts learned:
# - Private attributes (underscore convention)
# - Property decorators (@property)
# - Getter and setter methods
# - Data validation in setters
# - Encapsulation principles
#
# Try it yourself:
# 1. Start with Step 1 and code along
# 2. Test each step before moving to the next
# 3. Understand WHY each step is necessary
# 4. Experiment with modifications (try adding email validation!)
#
# Remember: The best way to learn is by doing!
# ===============================================================================