Trey Hunner - Lazy Looping in Python: Making and Using Generators and Iterators - PyCon 2019

preview_player
Показать описание
"Speaker: Trey Hunner

When processing large amounts of data in Python, we often reach for lists. Unfortunately, processing data using large lists make for ugly code that can be memory inefficient and slow. Python's solution to this problem is lazy looping using generators and iterators.

During this tutorial we'll learn a number of lazy looping techniques which will help you write more efficient and more readable Python code. We'll get practice creating generators, playing with iterators, and using generators and iterators to drastically restructure our code in a more descriptive data-centric way.

You'll walk out of this tutorial with hands-on experience with Python's various lazy looping constructs and a greater appreciation for how looping works under the hood in Python.

Рекомендации по теме
Комментарии
Автор

Learned a lot from this talk.. Very well presented

BuvanAlmighty
Автор

Really nice talk, this made this topic so much clearer for me

giandja
Автор

"generator expression" started at 1:02:37

gongfei
Автор

3:23:00 __iter__ __next__ convert a yield function to class OR why we need yield

usf
Автор

list comprehensions
7:51 filter with list comprehension

***until 2:16:38 will undrstand blow topics
!list :
[x**2 for x in range(10)]
[<append part of FOR LOOP> <FOR LOOP> <IF>]

!generator :
(x**2 for x in range(10))

!!to reduce memory usage, generate numbers when is needed
!!generator create a disposable lazy loop, that with " next function ", generate next item.
!!!
!! delete extra parantesis
print(x**2 for x in range(10))
sum(x**2 for x in range(10))

!converting generator to list
[x**2 for x in range(10)]
list(x**2 for x in range(10))
!! two above is equal

!tuple conversions
tuple(x**2 for x in range(10))



yield sign in traffic

def count(n=0):
print('start')
while n<4:
yield n
n += 1
print('loop')
print('end')



**return(item for iterable in iterables for item in iterable)

**for iterable in iterables:
for item in iterable:
yield item

**for iterable in iterables:
yield from iterable

!! three above is equal

**(<yield part of FOR LOOP> <FOR LOOP> <IF>)


!next only works on iterators
!!list is not an iterator
!!!generators are iterators

!iter functions in python
iter([1, 2, 3])

iterator protocol
iterator design pattern



any iterable (like string, list, ) can be passed to iter function to get an iterator from it.


def print_each(iterable):
for item in iterable:
print(item)


! how for loops work under the hood in python
def print_each(iterable):
iterator = iter(iterable)

while True:
try:
item = next(iterator)
except StopIteration:
return
else:
print(item)

usf