Apa itu generator dan cara membuatnya?

Berlatih Pertanyaan Wawancara Coding di Python

Kirill Smirnov

Data Science Consultant, Altran

Definisi

Generator - objek iterable khusus yang dibuat oleh fungsi yang memiliki kata kunci yield di dalam bodinya.

def func():
    # Return a value from super complex calculations
    return 0
result = func()
print(result)
0
Berlatih Pertanyaan Wawancara Coding di Python

Definisi

Generator - objek iterable khusus yang dibuat oleh fungsi yang memiliki kata kunci yield di dalam bodinya.

def func():
    # Yield a value from super complex calculations
    yield 0
result = func()
print(result)
<generator object result at 0x105736e10>
Berlatih Pertanyaan Wawancara Coding di Python

Generator sebagai Iterable

def func():
    # Yield a value from super complex calculations
    yield 0

result = func()
for item in result:
    print(item)
0
Berlatih Pertanyaan Wawancara Coding di Python

Lebih banyak yield!

def func():
    yield 0
    yield 1
    yield 2
result = func()
for item in result:
    print(item)
0
1
2
Berlatih Pertanyaan Wawancara Coding di Python

Yield dalam loop

def func(n):
    for i in range(0, n):
        yield 2*i
result = func(3)
for item in result:
    print(item)
0
2
4
Berlatih Pertanyaan Wawancara Coding di Python

Mengonversi generator ke list

def func(n):
    for i in range(0, n):
        yield 2*i

result = func(5)
list(result)
[0, 2, 4, 6, 8]
Berlatih Pertanyaan Wawancara Coding di Python

Generator sebagai Iterator

Generator adalah Iterable DAN Iterator

def func(n):
    for i in range(0, n):
        yield 2*i
result = func(3)
next(result)
0
next(result)
2
next(result)
4
next(result)
StopIteration
Berlatih Pertanyaan Wawancara Coding di Python

Generator habis pakai

def func(n):
    for i in range(0, n):
        yield 2*i
result = func(3)
for item in result:
    print(item)
0
2
4
for item in result:
    print(item)
# nothing
result = func(3)
for item in result:
    print(item)
0
2
4
Berlatih Pertanyaan Wawancara Coding di Python

Generator habis pakai

def func(n):
    for i in range(0, n):
        yield 2*i
result = func(3)
list(result)
[0, 2, 4]
list(result)
[]
result = func(3)
list(result)
[0, 2, 4]
Berlatih Pertanyaan Wawancara Coding di Python

Komprehensi generator

result = [2*i for i in range(0, 3)]
print(result)
[0, 2, 4]
result = (2*i for i in range(0, 3))
print(result)
<generator object result at 0x105736e10>
Berlatih Pertanyaan Wawancara Coding di Python

Penelusuran

result = (2*i for i in range(0, 3))
for item in result:
    print(item)
0
2
4
next(result)
StopIteration
Berlatih Pertanyaan Wawancara Coding di Python

Mengapa generator?

  • cara sederhana membuat objek iterable kustom

[1, 3, 2, 4, 3, 5]

def create_jump_sequence(n):
    for i in range(1, n-1):
        yield i
        yield i+2
jump_sequence = create_jump_sequence(5)
list(jump_sequence)
[1, 3, 2, 4, 3, 5]
Berlatih Pertanyaan Wawancara Coding di Python

Mengapa generator?

  • cara sederhana membuat objek iterable kustom
  • inisialisasi malas

[1, 3, 2, 4, 3, 5, 4, 6, 5, 7, ...]

def create_jump_sequence(n):
    for i in range(1, n-1):
        yield i
        yield i+2
jump_sequence = create_jump_sequence(500)
next(jump_sequence)
1
Berlatih Pertanyaan Wawancara Coding di Python

Mengapa generator?

  • cara sederhana membuat objek iterable kustom
  • inisialisasi malas
  • dapat membuat objek iterable tak hingga
def create_inf_generator():
    while True:
        yield 'I am infinite!'
inf_generator = create_inf_generator()
next(inf_generator)
I am infinite
Berlatih Pertanyaan Wawancara Coding di Python

Ayo berlatih!

Berlatih Pertanyaan Wawancara Coding di Python

Preparing Video For Download...