https://github.com/parisaalizadeh2003/custom_for_loop
This project defines a Python class CustomFor that manually iterates over any iterable using next(), applying a specified function to each element. In this example, it capitalizes and prints each character. It demonstrates the use of iterators in Python.
https://github.com/parisaalizadeh2003/custom_for_loop
iterable iterate iterator loop loops-and-iterations oop python
Last synced: about 1 year ago
JSON representation
This project defines a Python class CustomFor that manually iterates over any iterable using next(), applying a specified function to each element. In this example, it capitalizes and prints each character. It demonstrates the use of iterators in Python.
- Host: GitHub
- URL: https://github.com/parisaalizadeh2003/custom_for_loop
- Owner: ParisaAlizadeh2003
- Created: 2025-03-07T12:01:06.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-07T14:23:04.000Z (over 1 year ago)
- Last Synced: 2025-04-12T00:50:04.266Z (about 1 year ago)
- Topics: iterable, iterate, iterator, loop, loops-and-iterations, oop, python
- Language: Python
- Homepage:
- Size: 4.88 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# **CustomFor - A Simple Custom Iterator**
## **Overview**
`CustomFor` is a lightweight Python utility that demonstrates **manual iteration** using an explicit iterator. Instead of using a traditional `for` loop, it manually retrieves elements using `next()` and applies a function to each item.
πΉ Learn more about **Python Iterators** here: [Python Iterators](https://docs.python.org/3/tutorial/classes.html#iterators)
## **Features**
β
Custom iteration over any iterable
β
Applies a given function to each item
β
Includes a built-in `capitalize` function
## **Installation**
No external dependencies are required. Just clone the repository and run the script.
```bash
git clone https://github.com/YOUR-USERNAME/custom-for-loop.git
cd custom-for-loop
python custom_for.py
```
## **Usage**
Hereβs how the `CustomFor` class works:
```python
class CustomFor:
@staticmethod
def iterate(iterable, func):
iterator = iter(iterable)
while True:
try:
item = next(iterator)
except StopIteration:
return
else:
func(item)
@staticmethod
def capitalize(item):
print(str.capitalize(item))
def main():
CustomFor.iterate(input("Enter your iterable: "), CustomFor.capitalize)
if __name__ == "__main__":
main()
```
### **Example**
#### **Input:**
```
Enter your iterable: hello
```
#### **Output:**
```
H
E
L
L
O
```
## **Contributing**
Want to improve this project? Feel free to fork the repository and submit a pull request! π
## **License**
This project is licensed under the **MIT License**.
---