Skip to content

Latest commit

 

History

History
17 lines (16 loc) · 433 Bytes

25_yield.md

File metadata and controls

17 lines (16 loc) · 433 Bytes

If you find yourself writing a function like this:

def get_numbers(x): ... l = [] ... if x > 5: ... l.append(5) ... if x < 2: ... for i in range(0, x + 2): ... l.append(i) ... return l

You should use the yield keyword instead:

def get_numbers(x): ... if x > 5: ... yield 5 ... if x < 2: ... for i in range(0, x + 2): ... yield i