from collections import Iterable, Iterator
class EmployeesIterator(Iterator):
def __init__(self, employees, head_count):
self._employees = employees
self._head_count = head_count
self._emp_id = 0
def __iter__(self):
return self
def __next__(self):
if self._emp_id < self._head_count:
self._emp_id += 1
return self._employees[self._emp_id]
else:
raise StopIteration
class Employees(Iterable):
_employees = {}
_head_count = 0
_emp_id = 0
def add_employee(self, emp_id, employee):
self._head_count += 1
self._emp_id = emp_id
self._employees[self._emp_id] = employee
def __iter__(self):
return EmployeesIterator(self._employees, self._head_count)
test_employees = ['Ashutosh', 'Bipender', 'Sameer', 'Abhijit']
employees = Employees()
for i, emp in enumerate(test_employees):
employees.add_employee(i + 1, emp)
for e in employees:
print(e)
Ashutosh Bipender Sameer Abhijit
class Employees(Iterable):
_employees = {}
_head_count = 0
_emp_id = 0
def add_employee(self, emp_id, employee):
self._head_count += 1
self._emp_id = emp_id
self._employees[self._emp_id] = employee
def __iter__(self): # Or - return (e for e in self._employees.values())
for i in range(self._head_count):
yield self._employees[i + 1]
employees = Employees()
for i, emp in enumerate(test_employees):
employees.add_employee(i + 1, emp)
for e in employees:
print(f'"{e}"')
"Ashutosh" "Bipender" "Sameer" "Abhijit"