|
| 1 | +# ✅ Example 1: Global Variable Usage |
| 2 | +a = 5 |
| 3 | + |
| 4 | +def function(): |
| 5 | + global a # Use the global 'a' instead of creating a local one |
| 6 | + print("Before changing, a =", a) # Should print 5 |
| 7 | + a = 10 # Update global variable 'a' |
| 8 | + print("After changing, a =", a) # Should print 10 |
| 9 | + |
| 10 | +function() |
| 11 | +print("Global a after function call:", a) # Confirm global 'a' is now 10 |
| 12 | + |
| 13 | + |
| 14 | +# ✅ Example 2: Closure Example – Function Inside a Function |
| 15 | +def f1(): |
| 16 | + x = 88 # This is in the enclosing scope of f2 |
| 17 | + def f2(): |
| 18 | + print("Value from closure:", x) # f2 uses x from f1 |
| 19 | + return f2 # Return the inner function |
| 20 | + |
| 21 | +myResult = f1() # myResult now holds reference to f2, with access to x=88 |
| 22 | +myResult() # Should print: Value from closure: 88 |
| 23 | + |
| 24 | + |
| 25 | +# ✅ Example 3: Function Factory using Closure |
| 26 | +def chaicoder(num): # Outer function with parameter `num` |
| 27 | + def actual(x): # Inner function that uses `num` from outer scope |
| 28 | + return x ** num |
| 29 | + return actual # Return the inner function |
| 30 | + |
| 31 | +f = chaicoder(2) # f becomes a function that squares numbers |
| 32 | +g = chaicoder(3) # g becomes a function that cubes numbers |
| 33 | + |
| 34 | +print("Function f (square):", f) # Prints function reference |
| 35 | +print("Function g (cube):", g) # Prints function reference |
| 36 | + |
| 37 | +print("f(3) =", f(3)) # 3^2 = 9 |
| 38 | +print("g(3) =", g(3)) # 3^3 = 27 |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +# Closure to keep track of count |
| 43 | +def counter(): |
| 44 | + count = 0 |
| 45 | + def increment(): |
| 46 | + nonlocal count # Use 'nonlocal' to modify the outer function variable |
| 47 | + count += 1 |
| 48 | + return count |
| 49 | + return increment |
| 50 | + |
| 51 | +count1 = counter() |
| 52 | +print(count1()) # 1 |
| 53 | +print(count1()) # 2 |
| 54 | + |
| 55 | +count2 = counter() |
| 56 | +print(count2()) # 1 (separate instance) |
| 57 | +print(count1()) # 3 (continues from previous count1) |
| 58 | + |
| 59 | +#Add-On 2: Lambda with Closure |
| 60 | +def power_maker(n): |
| 61 | + return lambda x: x ** n # Returns an anonymous function that remembers n |
| 62 | + |
| 63 | +square = power_maker(2) |
| 64 | +cube = power_maker(3) |
| 65 | + |
| 66 | +print("square(4) =", square(4)) # 16 |
| 67 | +print("cube(2) =", cube(2)) # 8 |
| 68 | + |
| 69 | + |
| 70 | +#Add-On 3: Function Scope vs Global vs Nonlocal |
| 71 | + |
| 72 | +x = "global" |
| 73 | + |
| 74 | +def outer(): |
| 75 | + x = "outer" |
| 76 | + def inner(): |
| 77 | + nonlocal x |
| 78 | + x = "inner" |
| 79 | + print("Inner:", x) |
| 80 | + inner() |
| 81 | + print("Outer after inner:", x) |
| 82 | + |
| 83 | +outer() |
| 84 | +print("Global:", x) |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | +#Add-On 4: Common Mistake in Closures in Loops (Late Binding) |
| 89 | +functions = [] |
| 90 | + |
| 91 | +for i in range(3): |
| 92 | + def make_func(n): |
| 93 | + def f(): |
| 94 | + print(n) |
| 95 | + return f |
| 96 | + functions.append(make_func(i)) |
| 97 | + |
| 98 | +for fn in functions: |
| 99 | + fn() # Correct: prints 0, 1, 2 |
0 commit comments