Skip to content

Latest commit

 

History

History
44 lines (37 loc) · 934 Bytes

File metadata and controls

44 lines (37 loc) · 934 Bytes

Iterator

Definition

  • iterate : 반복하다

iterable Object

  • List, Set, Dictionary

How to make a 'Class' to be iterable

  • define _iter_ , _next_ (special method, DUNDER method)

For Loop

  • Iterable objects can be used at 'for loop' by calling 'next' method.
    • ex) "for a in ["1", "2", "3"]"

Generator

Definition

  • Generator is a special form of Iterator
  • Every generator should stop at yield, and yield give result partially whenever next method is called.

When to use

  • Data is too big to return at once, so need to return partially.

Example

  • sample code below
def generator():
   yield 1
   yield 2
   yield 3
g = generator()
print(type(g)) #class generator
n = next(g)
n = next(g)
n = next(g)
for x in generator():
    print(x)

Refer to