-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy path02_comments.py
More file actions
343 lines (253 loc) · 10.1 KB
/
02_comments.py
File metadata and controls
343 lines (253 loc) · 10.1 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
"""
================================================================================
File: 02_comments.py
Topic: Comments in Python
================================================================================
This file demonstrates how to write comments in Python. Comments are essential
for code documentation, making your code readable, and helping others (and
your future self) understand what the code does.
Key Concepts:
- Single-line comments
- Multi-line comments
- Docstrings
- Best practices for commenting
================================================================================
"""
# -----------------------------------------------------------------------------
# 1. Single-Line Comments
# -----------------------------------------------------------------------------
# Use the hash symbol (#) to create single-line comments
print("--- Single-Line Comments ---")
# This is a single-line comment
print("Hello, World!") # This is an inline comment
# Comments can explain complex logic
x = 10 # Store the initial value
y = 20 # Store the second value
result = x + y # Calculate the sum
# Comments can be used to organize sections of code
# ---- Configuration ----
debug_mode = True
max_retries = 3
# ---- Main Logic ----
print(f"Debug mode is: {debug_mode}")
# -----------------------------------------------------------------------------
# 2. Multi-Line Comments
# -----------------------------------------------------------------------------
# Python doesn't have true multi-line comments, but there are two approaches
print("\n--- Multi-Line Comments ---")
# Approach 1: Multiple single-line comments (preferred)
# This is a multi-line comment
# that spans across multiple lines.
# Each line starts with a hash symbol.
# Approach 2: Triple-quoted strings (not recommended for comments)
# These are actually string literals, not true comments
"""
This looks like a multi-line comment, but it's actually
a string literal. If not assigned to a variable,
Python will ignore it, but it's still stored in memory.
Use this approach only for docstrings.
"""
print("Multi-line comments explained above!")
# -----------------------------------------------------------------------------
# 3. Docstrings (Documentation Strings)
# -----------------------------------------------------------------------------
# Docstrings are special strings used to document modules, functions, classes
print("\n--- Docstrings ---")
def greet(name):
"""
Greet a person by their name.
This function takes a person's name and prints a friendly
greeting message to the console.
Args:
name (str): The name of the person to greet.
Returns:
str: A greeting message.
Examples:
>>> greet("Baraa")
'Hello, Baraa! Welcome!'
"""
return f"Hello, {name}! Welcome!"
# Access the docstring
print(greet("Baraa"))
print(f"Function docstring: {greet.__doc__[:50]}...")
def calculate_area(length, width):
"""
Calculate the area of a rectangle.
Args:
length (float): The length of the rectangle.
width (float): The width of the rectangle.
Returns:
float: The area of the rectangle.
Raises:
ValueError: If length or width is negative.
"""
if length < 0 or width < 0:
raise ValueError("Length and width must be non-negative")
return length * width
print(f"Area: {calculate_area(5, 3)}")
# -----------------------------------------------------------------------------
# 4. Class Docstrings
# -----------------------------------------------------------------------------
print("\n--- Class Docstrings ---")
class Rectangle:
"""
A class to represent a rectangle.
This class provides methods to calculate the area and perimeter
of a rectangle, as well as other utility methods.
Attributes:
length (float): The length of the rectangle.
width (float): The width of the rectangle.
Methods:
area(): Returns the area of the rectangle.
perimeter(): Returns the perimeter of the rectangle.
"""
def __init__(self, length, width):
"""
Initialize a Rectangle instance.
Args:
length (float): The length of the rectangle.
width (float): The width of the rectangle.
"""
self.length = length
self.width = width
def area(self):
"""Calculate and return the area of the rectangle."""
return self.length * self.width
def perimeter(self):
"""Calculate and return the perimeter of the rectangle."""
return 2 * (self.length + self.width)
rect = Rectangle(10, 5)
print(f"Rectangle area: {rect.area()}")
print(f"Rectangle perimeter: {rect.perimeter()}")
# -----------------------------------------------------------------------------
# 5. Comment Best Practices
# -----------------------------------------------------------------------------
print("\n--- Comment Best Practices ---")
# ✅ GOOD: Explain WHY, not WHAT
# Calculate discount for loyalty members (15% off for 2+ years)
years_as_member = 3
discount = 0.15 if years_as_member >= 2 else 0
# ❌ BAD: Explains what the code obviously does
# x = x + 1 # Add 1 to x
# ✅ GOOD: Document complex algorithms
# Using binary search for O(log n) time complexity
# instead of linear search O(n) for performance
# ✅ GOOD: Mark TODO items for future work
# TODO: Implement caching for better performance
# FIXME: Handle edge case when input is empty
# NOTE: This function requires Python 3.8+
# HACK: Temporary workaround for API limitation
# ✅ GOOD: Use comments for code sections
# ============================================
# DATABASE CONNECTION SETUP
# ============================================
# ============================================
# USER AUTHENTICATION
# ============================================
print("Best practices demonstrated!")
# -----------------------------------------------------------------------------
# 6. Commenting Out Code
# -----------------------------------------------------------------------------
print("\n--- Commenting Out Code ---")
# You can temporarily disable code by commenting it out
# print("This line won't execute")
# old_function()
# deprecated_code()
# Useful during debugging
value = 100
# value = 200 # Uncomment to test with different value
print(f"Current value: {value}")
# Multiple lines can be commented at once
# line_1 = "first"
# line_2 = "second"
# line_3 = "third"
# -----------------------------------------------------------------------------
# 7. Module-Level Docstrings
# -----------------------------------------------------------------------------
print("\n--- Module-Level Docstrings ---")
# At the top of a Python file, you can include a module docstring
# (like the one at the top of this file)
# Access a module's docstring
print("This module's docstring starts with:")
print(__doc__[:100] + "...")
# -----------------------------------------------------------------------------
# 8. Type Hints with Comments
# -----------------------------------------------------------------------------
print("\n--- Type Hints with Comments ---")
def process_data(
data: list, # The input data to process
threshold: float = 0.5, # Minimum value to include (default: 0.5)
verbose: bool = False # Print progress if True
) -> list:
"""
Process a list of numerical data.
Args:
data: List of numbers to process.
threshold: Minimum value to include in results.
verbose: Whether to print processing details.
Returns:
Filtered list containing only values above threshold.
"""
if verbose:
print(f"Processing {len(data)} items...")
return [x for x in data if x > threshold]
result = process_data([0.1, 0.6, 0.3, 0.8, 0.4], threshold=0.5)
print(f"Filtered data: {result}")
# -----------------------------------------------------------------------------
# 9. Practical Example: Well-Commented Code
# -----------------------------------------------------------------------------
print("\n--- Practical Example ---")
def calculate_shipping_cost(weight, distance, express=False):
"""
Calculate the shipping cost based on weight and distance.
The cost is calculated using a base rate plus additional charges
for weight and distance. Express shipping doubles the final cost.
Args:
weight (float): Package weight in kilograms.
distance (float): Shipping distance in kilometers.
express (bool): Whether to use express shipping.
Returns:
float: Total shipping cost in dollars.
Example:
>>> calculate_shipping_cost(2.5, 100)
17.0
>>> calculate_shipping_cost(2.5, 100, express=True)
34.0
"""
# Base shipping rate
BASE_RATE = 5.00
# Rate per kilogram of weight
WEIGHT_RATE = 2.00
# Rate per 100 kilometers
DISTANCE_RATE = 0.05
# Calculate component costs
weight_cost = weight * WEIGHT_RATE
distance_cost = distance * DISTANCE_RATE
# Sum up total cost
total = BASE_RATE + weight_cost + distance_cost
# Apply express multiplier if applicable
if express:
total *= 2 # Express shipping is 2x the normal rate
return round(total, 2)
# Example usage
package_weight = 3.5 # kg
shipping_distance = 250 # km
standard_cost = calculate_shipping_cost(package_weight, shipping_distance)
express_cost = calculate_shipping_cost(package_weight, shipping_distance, express=True)
print(f"Standard shipping: ${standard_cost}")
print(f"Express shipping: ${express_cost}")
# -----------------------------------------------------------------------------
# Summary
# -----------------------------------------------------------------------------
print("\n" + "=" * 60)
print("COMMENTS SUMMARY")
print("=" * 60)
print("""
1. Single-line: Use # for short comments
2. Multi-line: Use multiple # lines
3. Docstrings: Use triple quotes for documentation
4. Explain WHY, not WHAT
5. Keep comments up-to-date with code changes
6. Use TODO, FIXME, NOTE for special markers
7. Don't over-comment obvious code
""")