Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b98c783

Browse files
author
Rai Muhammad Haider
committed
Scope
1 parent cd169d3 commit b98c783

File tree

2 files changed

+66
-80
lines changed

2 files changed

+66
-80
lines changed

‎Loops.py

Lines changed: 2 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191

9292
print('-' * 50)
9393

94-
# Retry system with exponential wait if some one try it will wait again twice
94+
# Retry system with exponential wait
9595
import time
9696
wait_time = 1
9797
max_retries = 5
@@ -100,82 +100,4 @@
100100
print('Attempt', attempts + 1, '- wait time', wait_time, 'seconds')
101101
time.sleep(wait_time)
102102
wait_time *= 2
103-
attempts += 1
104-
#################################### Behind the Seen #####################################################################
105-
# Topic: Behind the Scene - Iterable, Iterator, Iteration Tool
106-
import time
107-
108-
print('He is my friend')
109-
string = 'haider'
110-
print(string
111-
# ----------------------------- Explanation -----------------------------
112-
113-
# Iteration tools:
114-
# These are constructs used to perform iteration (looping).
115-
# Examples: for loop, while loop.
116-
117-
# Iterable objects:
118-
# These are objects that can be looped over (i.e., used with iteration tools).
119-
# Examples: list, tuple, string, dictionary, file, set.
120-
# A true iterable must have the __iter__() method defined.
121-
122-
# Iterator:
123-
# An object with a __next__() method, which returns the next item from the iterable.
124-
# You can get an iterator from an iterable by using the iter() function.
125-
126-
# How iteration works (step-by-step):
127-
# 1. When a for loop is run on an iterable object, Python internally calls iter(iterable).
128-
# 2. This returns an iterator object with a __next__() method.
129-
# 3. Python keeps calling next(iterator) to get the next item in the sequence.
130-
# 4. When there are no more items left, a StopIteration exception is raised to end the loop.
131-
132-
# Key Concepts:
133-
# - The iter() function gives an iterator from an iterable object.
134-
# - The next() function fetches the next item from the iterator.
135-
# - Once all items are consumed, next() raises StopIteration.
136-
# - The iterator remembers its current position internally.
137-
# - The memory address of the iterator remains constant, but the pointer moves to the next item.
138-
139-
# Special Note on Files:
140-
# When we open a file using open(), it returns a file object which is also an iterable.
141-
# You can use iter() and next() on it directly to read line-by-line.
142-
143-
# Example:
144-
# my_list = [1, 2, 3]
145-
# iterator = iter(my_list)
146-
# print(next(iterator)) # Output: 1
147-
# print(next(iterator)) # Output: 2
148-
# print(next(iterator)) # Output: 3
149-
# print(next(iterator)) # Raises StopIteration
150-
151-
# ---------------------------- Notes in Urdu ----------------------------
152-
153-
# Iteration tools wo hoty hain jo iterate karwate hain
154-
# Example: for loop, while loop
155-
156-
# Iterable objects wo hoty hain jin ko hum iterate kar sakte hain
157-
# Example: list, file, dictionary
158-
159-
# Jab hum kisi iterable object ko iterate karte hain, to response milta hai
160-
# Aur ye response deta hai ek iterator object jisme __next__ method hoti hai
161-
162-
# Iterable object memory address return karta hai jahan se iteration start hoti hai
163-
164-
# iter() function iterable object par apply hota hai
165-
# Ye iterable object ko kehta hai: "mujhe iterate karne do"
166-
# Result me ye iterator object return karta hai (with memory address)
167-
168-
# __next__ or next() ka use karke hum agla element get karte hain
169-
# Har bar next() call karne par agla element return hota hai
170-
171-
# Jab sari values iterate ho jati hain, to StopIteration exception raise hoti hai
172-
# Ye indicate karta hai ke aur koi element baqi nahi hai
173-
174-
# Important Point:
175-
# Memory address same rehta hai, lekin pointer agay move karta hai
176-
177-
# File variable jab kisi file ko open karta hai to wo ek iterable object ban jata hai
178-
179-
# Agar hum list ka sirf reference variable banayein (e.g., my_list = [1, 2, 3])
180-
# To wo iterator nahi hota — iterable object hota hai, lekin iterator nahi
181-
# Iterator banane ke liye iter(my_list) use karna padta hai
103+
attempts += 1

‎ScopeClosure.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# 🔵 Global Scope Example
2+
globe = ' I am Global Variable'
3+
def outer():
4+
x = ' I am X (defined in outer)'
5+
def inner():
6+
y = 'I am Y (defined in inner)'
7+
def most_inner():
8+
z = '📦 I am Z (defined in most_inner)'
9+
def last_inner():
10+
last = ' I am LAST (defined in last_inner)'
11+
print('--- Inside last_inner ---')
12+
print(last)
13+
last_inner()
14+
print('--- Inside most_inner ---')
15+
print(globe) # Accessing global variable
16+
print(x) # Accessing from outer
17+
# print(y) # Uncomment to access y (from inner)
18+
# print(z) # Uncomment to access z (from same function)
19+
most_inner()
20+
print('--- Inside inner ---')
21+
print(y)
22+
inner()
23+
print('--- Inside outer ---')
24+
print(x)
25+
#Run the function chain
26+
outer()
27+
# Closure Example
28+
# Function returns another function that remembers the value of counter
29+
# A closure is when an inner function remembers the values from its outer function even after the outer function has finished.
30+
def counter_banao():
31+
counter = 0
32+
def gino():
33+
nonlocal counter # This allows us to modify 'counter' from outer function
34+
counter += 1
35+
return counter
36+
return gino # We return the function, not call it
37+
# Create a new counter instance
38+
ginti = counter_banao()
39+
# Each call to ginti() will remember the previous count
40+
print("Counter Example:")
41+
print(ginti()) # 1
42+
print(ginti()) # 2
43+
print(ginti()) # 3
44+
45+
46+
# Scope Example
47+
name = " I'm Global"
48+
49+
def outer_func():
50+
outer_var = " I'm from Outer"
51+
52+
def inner_func():
53+
inner_var = " I'm from Inner"
54+
print("Accessing everything inside inner_func:")
55+
print(name) # Global
56+
print(outer_var) # From outer_func
57+
print(inner_var) # From inner_func
58+
59+
inner_func()
60+
61+
outer_func()
62+
63+
64+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /