-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12-class-vs-instance-attributes.py
More file actions
349 lines (241 loc) · 9.35 KB
/
12-class-vs-instance-attributes.py
File metadata and controls
349 lines (241 loc) · 9.35 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
"""Question: Create a class Document with a class attribute document_count
to keep track of the number of documents created.
Add methods to read and write content to the document.
Use class methods to get the document count.
"""
# 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 class attributes? (shared by all instances)
# - When should you increment the counter? (in __init__)
# - How do you access class attributes? (ClassName.attribute or cls.attribute)
# - What's the difference between instance and class methods?
#
# 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 Document class with class attribute
# ===============================================================================
# Explanation:
# Let's start by creating our Document class with a class attribute to track
# the total number of documents created. Class attributes are shared by all instances.
class Document:
document_count = 0
# What we accomplished in this step:
# - Created Document class with class attribute document_count
# Step 2: Add constructor that increments the counter
# ===============================================================================
# Explanation:
# The __init__ method should increment the document count each time a new
# document is created. We'll also initialize the document with title and content.
class Document:
document_count = 0
def __init__(self, title, content=""):
self.title = title
self.content = content
Document.document_count += 1
# What we accomplished in this step:
# - Added constructor that increments document_count
# - Initialized title and content attributes
# Step 3: Add read method
# ===============================================================================
# Explanation:
# The read method should return the current content of the document.
# This provides a clean interface for accessing document content.
class Document:
document_count = 0
def __init__(self, title, content=""):
self.title = title
self.content = content
Document.document_count += 1
def read(self):
return self.content
# What we accomplished in this step:
# - Added read method to access document content
# Step 4: Add write method
# ===============================================================================
# Explanation:
# The write method should update the document's content.
# We can also add some additional functionality like tracking modifications.
class Document:
document_count = 0
def __init__(self, title, content=""):
self.title = title
self.content = content
self.modified = False
Document.document_count += 1
def read(self):
return self.content
def write(self, content):
self.content = content
self.modified = True
# What we accomplished in this step:
# - Added write method to update document content
# - Added modified flag to track changes
# Step 5: Add class method to get document count
# ===============================================================================
# Explanation:
# Class methods take 'cls' as the first parameter and can access class attributes.
# This provides a clean interface for accessing the document count.
class Document:
document_count = 0
def __init__(self, title, content=""):
self.title = title
self.content = content
self.modified = False
Document.document_count += 1
def read(self):
return self.content
def write(self, content):
self.content = content
self.modified = True
@classmethod
def get_document_count(cls):
return cls.document_count
# What we accomplished in this step:
# - Added class method to access document count
# Step 6: Add additional useful methods
# ===============================================================================
# Explanation:
# Let's add some additional methods to make our Document class more useful,
# including methods to append content and clear the document.
class Document:
document_count = 0
def __init__(self, title, content=""):
self.title = title
self.content = content
self.modified = False
Document.document_count += 1
def read(self):
return self.content
def write(self, content):
self.content = content
self.modified = True
def append(self, content):
self.content += content
self.modified = True
def clear(self):
self.content = ""
self.modified = True
def get_length(self):
return len(self.content)
@classmethod
def get_document_count(cls):
return cls.document_count
@classmethod
def reset_count(cls):
cls.document_count = 0
# What we accomplished in this step:
# - Added append method to add content
# - Added clear method to empty document
# - Added get_length method for content length
# - Added reset_count class method for testing
# Step 7: Add string representation and test the class
# ===============================================================================
# Explanation:
# Let's add a __str__ method and test our complete Document implementation
# to make sure the class attribute tracking works correctly.
class Document:
document_count = 0
def __init__(self, title, content=""):
self.title = title
self.content = content
self.modified = False
Document.document_count += 1
def read(self):
return self.content
def write(self, content):
self.content = content
self.modified = True
def append(self, content):
self.content += content
self.modified = True
def clear(self):
self.content = ""
self.modified = True
def get_length(self):
return len(self.content)
@classmethod
def get_document_count(cls):
return cls.document_count
@classmethod
def reset_count(cls):
cls.document_count = 0
def __str__(self):
status = "Modified" if self.modified else "Unmodified"
return f"Document(title='{self.title}', length={self.get_length()}, {status})"
# Test our class:
print("Testing Document class with class attribute tracking:")
print(f"Initial document count: {Document.get_document_count()}")
# Create first document
doc1 = Document("Report 1", "Initial content for report 1")
print(f"After creating doc1: {Document.get_document_count()}")
print(f"Doc1: {doc1}")
# Create second document
doc2 = Document("Report 2")
print(f"After creating doc2: {Document.get_document_count()}")
print(f"Doc2: {doc2}")
# Test read and write operations
print(f"\nReading doc1: '{doc1.read()}'")
doc2.write("This is the content for report 2")
print(f"After writing to doc2: {doc2}")
# Create third document
doc3 = Document("Report 3", "Some initial content")
print(f"After creating doc3: {Document.get_document_count()}")
# Test append functionality
doc3.append(" - Additional content added")
print(f"After appending to doc3: {doc3}")
print(f"Doc3 content: '{doc3.read()}'")
# Test that count is shared across all instances
print(f"\nFinal document count: {Document.get_document_count()}")
print(f"Count accessed via doc1: {doc1.get_document_count()}")
print(f"Count accessed via doc2: {doc2.get_document_count()}")
print(f"Count accessed via doc3: {doc3.get_document_count()}")
# What we accomplished in this step:
# - Created and tested our complete Document implementation
# - Demonstrated class attribute sharing across instances
# ===============================================================================
# CONGRATULATIONS!
#
# You've successfully completed the step-by-step solution!
#
# Key concepts learned:
# - Class attributes vs instance attributes
# - Automatic counting with class attributes
# - Class methods for accessing shared data
# - Document management operations (read, write, append, clear)
# - Shared state across all instances of a class
#
# 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 document categories or creation timestamps!)
#
# Remember: The best way to learn is by doing!
# ===============================================================================