build-app-with-python-antitextbook icon indicating copy to clipboard operation
build-app-with-python-antitextbook copied to clipboard

Add list comprehension and generators

Open gdassori opened this issue 8 years ago • 2 comments

A mention could be deserved by list comprensions and generators (on which an entire chapter, maybe, should be done).

Specifically, you may want to explain the difference between

[requests.get('http://website.com/page/{}'.format(x) for x in range(0,10)]

and

(requests.get('http://website.com/page/{}'.format(x) for x in range(0,10))

which is a very powerful example on how generators may help during software development.

gdassori avatar Feb 11 '17 13:02 gdassori

Do you think a chapter on list comprehension and generators would make sense for an introductory tutorial? I think I have explained list comprehensions, am not totally sure about generators.

thewhitetulip avatar Feb 11 '17 16:02 thewhitetulip

I wouldn't spend too much on list comprehension, just some examples. Instead an entire chapter on generators would be super-useful, also given the asyncio reactor you may would approach in the future.

Make clear the use of 'yield' and implement yourself a generator, I would look at static variables also to explain "where" this data is saved.

def foo():
    foo.counter += 1
    print "Counter is %d" % foo.counter
foo.counter = 0

I remember generators was probably the hard part at all to understand in python, to me, and this small snippet I think explains a lot.

gdassori avatar Feb 14 '17 07:02 gdassori