Skip to content

Commit 70bc2d5

Browse files
Manual review of builtin functions from beginning to issubclass
Mostly removing content that has nothing to do with performance.
1 parent 189b496 commit 70bc2d5

10 files changed

Lines changed: 25 additions & 184 deletions

File tree

docs/builtins/chr.md

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -210,18 +210,6 @@ except TypeError:
210210
pass
211211
```
212212

213-
## Surrogates and Edge Cases
214-
215-
```python
216-
# Valid surrogate pairs (combined characters)
217-
# These are valid Unicode code points
218-
chr(0xDC00) # Surrogate character (valid as standalone)
219-
220-
# But combining characters exist
221-
chr(0x0301) # Combining acute accent
222-
# Used with other characters: 'e' + chr(0x0301) = 'é'
223-
```
224-
225213
## Performance Considerations
226214

227215
### vs String Literals
@@ -259,15 +247,11 @@ result = ''.join(map(chr, codes))
259247

260248
**Do**:
261249

262-
- Use `chr()` to create characters from code points
263-
- Use with `ord()` for bidirectional conversion
264250
- Use `map(chr, codes)` for bulk conversion
265-
- Use for encoding/decoding operations
266251

267252
**Avoid**:
268253

269254
- Creating strings character by character (use join)
270-
- Assuming ASCII-only (support Unicode)
271255
- Using string literals when code points are computed
272256
- Unnecessary intermediate lists
273257

docs/builtins/divmod.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,14 +269,10 @@ for i in range(large_count):
269269
- Use `divmod()` when you need both quotient and remainder
270270
- Use for base conversion algorithms
271271
- Use for time calculations and pagination
272-
- Remember floor division semantics for negatives
273272

274273
**Avoid**:
275274

276275
- Computing `q` and `r` separately when you need both
277-
- Forgetting about negative number behavior
278-
- Assuming truncation toward zero (Python uses floor)
279-
- Dividing by zero without checking
280276

281277
## Related Functions
282278

docs/builtins/iter.md

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,6 @@ The `iter()` function creates an iterator object from an iterable, and `next()`
1515

1616
## Creating Iterators
1717

18-
### Basic Iterator Creation
19-
20-
```python
21-
# Create iterator from list - O(1)
22-
lst = [1, 2, 3, 4, 5]
23-
it = iter(lst) # O(1) - returns list iterator
24-
25-
# Create iterator from string - O(1)
26-
s = "hello"
27-
it = iter(s) # O(1) - returns string iterator
28-
29-
# Create iterator from dict - O(1)
30-
d = {'a': 1, 'b': 2}
31-
it = iter(d) # O(1) - returns dict_keyiterator
32-
33-
# Create iterator from set - O(1)
34-
st = {1, 2, 3}
35-
it = iter(st) # O(1) - returns set iterator
36-
```
37-
3818
### Iterating Over Sequences
3919

4020
```python
@@ -260,6 +240,18 @@ for it in [it1, it2, it3]:
260240
break
261241
```
262242

243+
### Consuming First Item Separately
244+
245+
```python
246+
# Use iter() explicitly when you need to advance past some items manually
247+
data = ["header", "row1", "row2", "row3"]
248+
it = iter(data)
249+
250+
header = next(it) # O(1) - consume first item separately
251+
for row in it: # O(n) - continues from second item
252+
print(f"Row: {row}")
253+
```
254+
263255
### Zip with Iterators
264256

265257
```python

docs/builtins/min.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ lst = list(range(1000000))
5858
result = min(lst) # ~1M comparisons
5959

6060
# Same complexity with generators
61-
result = min(x**2 for x in range(10000)) # O(n)
61+
result = min(x**2 for x in range(10000)) # O(n) time, O(1) space
6262
```
6363

6464
### Working with Different Types
@@ -109,6 +109,7 @@ shortest = min(words, key=len) # "code"
109109
# O(n) - compare tuples lexicographically
110110
coords = [(1, 5), (3, 2), (2, 8)]
111111
min_coord = min(coords) # (1, 5) - compared by first element
112+
# If first equal, compares second element, etc.
112113

113114
# Compare by second element instead
114115
min_by_y = min(coords, key=lambda c: c[1]) # (3, 2)

docs/builtins/ord.md

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -190,18 +190,6 @@ except TypeError:
190190

191191
## Performance Considerations
192192

193-
### vs ASCII Codes
194-
195-
```python
196-
# ord() - works with all Unicode
197-
result = ord('') # 8364
198-
199-
# ASCII - only 0-127
200-
import string
201-
if ord('a') in range(128):
202-
print("ASCII character")
203-
```
204-
205193
### Bulk Operations
206194

207195
```python
@@ -218,17 +206,11 @@ codes = list(map(ord, text)) # O(n) - slightly more efficient
218206

219207
**Do**:
220208

221-
- Use `ord()` for getting character code points
222-
- Use with `chr()` for bidirectional conversion
223209
- Use `map(ord, string)` for bulk operations
224-
- Use for encoding/decoding operations
225210

226211
**Avoid**:
227212

228213
- Using `ord()` for character type checking (use str methods)
229-
- Assuming ASCII-only (support Unicode)
230-
- Passing multiple characters or strings
231-
- Building unnecessary lookup tables
232214

233215
## Related Functions
234216

docs/builtins/pow.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ except ZeroDivisionError:
209209

210210
- **[abs()](abs.md)** - Absolute value
211211
- **math.pow()** - Float-only power
212-
- **[** operator** - Exponentiation (equivalent)
212+
- **operator\*\*** - Exponentiation (equivalent)
213213
- **math.isqrt()** - Integer square root
214214

215215
## Comparison with Alternatives

docs/builtins/reversed.md

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -158,38 +158,6 @@ for item in items[::-1]:
158158
# reversed() is more memory efficient
159159
```
160160

161-
### reversed() vs Reverse Method
162-
163-
```python
164-
# List has reverse() method
165-
items = [1, 2, 3, 4, 5]
166-
items.reverse() # O(n) in-place, modifies original
167-
168-
# reversed() doesn't modify original
169-
items = [1, 2, 3, 4, 5]
170-
for item in reversed(items):
171-
process(item)
172-
# Original unchanged
173-
174-
# Use reversed() to preserve original
175-
# Use reverse() to modify in-place
176-
```
177-
178-
### reversed() vs Sorting
179-
180-
```python
181-
# reversed() preserves order
182-
items = [3, 1, 4, 1, 5, 9, 2, 6]
183-
result = list(reversed(items))
184-
# [6, 2, 9, 5, 1, 4, 1, 3]
185-
186-
# sorted with reverse=True sorts
187-
result = sorted(items, reverse=True)
188-
# [9, 6, 5, 4, 3, 2, 1, 1]
189-
190-
# Different operations - don't confuse them
191-
```
192-
193161
## Supported Types
194162

195163
### Works With
@@ -204,29 +172,6 @@ range_rev = reversed(range(1, 4)) # O(1)
204172
# All work efficiently
205173
```
206174

207-
### Doesn't Work With
208-
209-
```python
210-
# Types without __reversed__ method
211-
try:
212-
reversed({1, 2, 3}) # TypeError - sets
213-
except TypeError:
214-
pass
215-
216-
try:
217-
reversed({'a': 1, 'b': 2}) # TypeError - dicts
218-
except TypeError:
219-
pass
220-
221-
try:
222-
reversed(42) # TypeError - integers
223-
except TypeError:
224-
pass
225-
226-
# But you can iterate them differently
227-
reversed(list(my_set)) # Convert first
228-
```
229-
230175
## Edge Cases
231176

232177
### Empty Sequence

docs/builtins/round.md

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -177,20 +177,6 @@ round(1e20, 2) # 1e+20
177177
round(123456789.123, 2) # 123456789.12
178178
```
179179

180-
## Floating Point Pitfalls
181-
182-
```python
183-
# Be aware of float representation issues
184-
round(2.675, 2) # 2.67 (not 2.68!)
185-
# Float representation: 2.675 is actually 2.6749999...
186-
187-
# For exact rounding use Decimal
188-
from decimal import Decimal, ROUND_HALF_UP
189-
190-
result = float(Decimal("2.675").quantize(Decimal("0.01"), rounding=ROUND_HALF_UP))
191-
# 2.68 - exact
192-
```
193-
194180
## Performance Notes
195181

196182
```python
@@ -209,17 +195,10 @@ t_format = timeit.timeit(lambda: f"{3.14159:.2f}", number=10**7)
209195

210196
**Do**:
211197

212-
- Use `round()` for numeric rounding
213-
- Use `Decimal` for financial calculations
214198
- Use `f"{x:.2f}"` for display formatting
215-
- Remember banker's rounding behavior
216-
- Use negative digits for rounding to powers of 10
217199

218200
**Avoid**:
219201

220-
- Using `round()` for financial calculations (precision issues)
221-
- Assuming traditional rounding (0.5 rounds up) - Python uses banker's
222-
- Relying on exact float precision
223202
- Using `round()` for display (use format strings)
224203

225204
## Related Functions

docs/builtins/sorted.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ result = sorted(original)
253253
original = [3, 1, 4, 1, 5]
254254
original.sort()
255255

256-
# Both: O(n log n) time, O(n) space for Timsort
256+
# Both: O(n log n) time, O(n) space for Timsort/Powersort
257257
# Choose based on whether you need original
258258
```
259259

@@ -290,7 +290,7 @@ result = sorted([42])
290290
### Already Sorted
291291

292292
```python
293-
# O(n) - Timsort detects and handles efficiently
293+
# O(n) - Timsort/Powersort detects and handles efficiently
294294
numbers = list(range(1000000))
295295
result = sorted(numbers) # Nearly O(n)
296296
```

docs/builtins/sum.md

Lines changed: 8 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ total = sum([x for x in range(1000000)]) # O(n) time, O(n) space
4848
### Counting Items
4949

5050
```python
51-
# O(n) - count 1s
51+
# O(n) time, O(n) space - list comprehension
5252
count = sum([1 for x in range(100) if x % 2 == 0]) # 50 even numbers
5353

54-
# Better: use len() or sum with boolean
55-
count = sum(1 for x in range(100) if x % 2 == 0) # Still O(n)
54+
# Better: sum with boolean (True=1, False=0) - O(n) time, O(1) space
55+
count = sum(x % 2 == 0 for x in range(100)) # 50 even numbers
5656
```
5757

5858
### Computing Statistics
@@ -70,17 +70,17 @@ average = sum(numbers) / len(numbers) # Iterate twice
7070
### Flattening Lists
7171

7272
```python
73-
# O(n²) - inefficient!
74-
# Each sum call is O(n), and we have n lists
73+
# O(n*k) - inefficient! n=number of lists, k=items per list
74+
# Each + creates a new list and copies all accumulated items
7575
nested = [[1, 2], [3, 4], [5, 6]]
76-
flat = sum(nested, []) # O(n²) - avoid this!
76+
flat = sum(nested, []) # O(n*k) - avoid this!
7777

7878
# Better approaches:
79-
# O(n) - single iteration
79+
# O(n*k) total items, but no repeated copying
8080
from itertools import chain
8181
flat = list(chain(*nested))
8282

83-
# O(n) - unpacking
83+
# O(n*k) - single pass, no repeated copying
8484
flat = [item for sublist in nested for item in sublist]
8585
```
8686

@@ -174,40 +174,6 @@ total = sum(numbers) # 15
174174
totals = list(accumulate(numbers)) # [1, 3, 6, 10, 15]
175175
```
176176

177-
## Pitfalls
178-
179-
### Floating Point Precision
180-
181-
```python
182-
# Summing floats can lose precision
183-
numbers = [0.1] * 10
184-
print(sum(numbers)) # 1.0000000000000007, not exactly 1.0
185-
186-
# Better: use decimal for financial calculations
187-
from decimal import Decimal
188-
numbers = [Decimal('0.1')] * 10
189-
print(sum(numbers, Decimal('0'))) # Exact: 1.0
190-
```
191-
192-
### Empty Sequence
193-
194-
```python
195-
# sum() of empty is 0
196-
result = sum([]) # 0
197-
198-
# Or start value
199-
result = sum([], start=100) # 100
200-
201-
# But TypeError with strings if no start
202-
try:
203-
sum(["a", "b"]) # TypeError
204-
except TypeError:
205-
pass
206-
207-
# Works with start=""
208-
result = sum(["a", "b"], start="") # "ab"
209-
```
210-
211177
## Performance Notes
212178

213179
```python
@@ -233,15 +199,11 @@ fsum([0.1] * 10) # More precise for floats
233199

234200
- Use `sum()` for numeric aggregation
235201
- Use generator expressions with `sum()` for memory efficiency
236-
- Use `sum([], start=[])` with explicit start value
237-
- Use `math.fsum()` when precision matters
238202

239203
**Avoid**:
240204

241205
- `sum(nested_lists, [])` - O(n²) concatenation
242206
- `sum(strings, "")` - inefficient string concatenation
243-
- Forgetting start value (causes TypeError with non-numeric sequences)
244-
- Summing booleans when `any()` or `all()` is clearer
245207

246208
## Related Functions
247209

0 commit comments

Comments
 (0)