![]() |
|
[Tut] Yield Keyword in Python – A Simple Illustrated Guide - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Yield Keyword in Python – A Simple Illustrated Guide (/thread-97658.html) |
[Tut] Yield Keyword in Python – A Simple Illustrated Guide - xSicKxBot - 10-09-2020 Yield Keyword in Python – A Simple Illustrated Guide Introduction To [b]yield In Python[/b] While using a function, we generally use the Code: returnCode: yieldCode: yieldExample: Code: def counter(): x = 1 while x <= 5: yield x x += 1 for y in counter(): print(y)Output: Code: 1
2
3
4
5To understand the usage of yield keyword, you have to understand what are:
So let us discuss generators and Iterables before diving into the Code: yieldIterables An iterable is an object in Python from which we can get an iterator. For example, when a list is created, all its items can be iterated one by one. Thus, reading the items of the list one by one is known as iteration while the list is iterable. In Python, string, lists, sets, tuples, and dictionariesare iterable containers from which we can get an iterator. Example: Code: name = "FINXTER"
li = [1,2,3]
tup = (4,5,6)
s = {"A","B","C"}
d = {"a":100,"b":200,"c":300} print("\nIterating over String:")
for x in name: print(x, end=", ")
print("\nIterating over list:")
for x in li: print(x, end=" ")
print("\nIterating over tuple:")
for x in tup: print(x, end=" ")
print("\nIterating over set:")
for x in s: print(x, end=" ")
print("\nIterating over dictionary:")
for x in d: print(d[x], end=" ")Output: Code: Iterating over String:
F, I, N, X, T, E, R, Iterating over list:
1 2 3 Iterating over tuple:
4 5 6 Iterating over set:
A C B Iterating over dictionary:
100 200 300So we know, what is an iterable object. But what is an iterator? ❖ [b]Iterator[/b] Simply put, an iterator is any object that can be iterated upon. Iterators are implemented using loops. Iterators implement the following methods which are known as iterator protocols:
Let us have a look at the following program how we can iterate through an iterator in Python using the iterator protocol. Example: Returning an iterator from a list(iterable) and printing each value one by one: Code: li = [1,2,3,4,5]
it = iter(li) print(next(it))
print(next(it))
print(next(it))
print(next(it))
print(next(it))Output: Code: 1
2
3
4
5Now that brings us to the question, what’s the difference between an iterator and iterable? Here’s a one-liner to answer that: Quote: Every Iterator is an iterable, but every iterable is not an iterator. For example, a list is an iterable but it is not an iterator. We can create an iterator from an iterable object using the iterable object as shown above. ❖ [b]Creating Iterator Objects[/b] As mentioned earlier, the Code: __iter__()Code: __next__()Example: The following program demonstrates the creation of an iterator that returns a sequence of numbers starting from 100 and each iteration will increase the value by 100. Code: class IterObj: def __iter__(self): self.value = 100 return self def __next__(self): x = self.value self.value += 100 return x obj = IterObj()
it = iter(obj) print(next(it))
print(next(it))
print(next(it))Output: Code: 100
200
300The above program will continue to print forever if you keep using the Code: next()Code: StopIteration❖ [b]StopIteration[/b] Once the iteration is done for a specific number of times, we can define a terminating condition that raises an error once the desired number of iterations are over. This terminating condition is given by the StopIteration statement. Example: Code: class IterObj: def __iter__(self): self.value = 100 return self def __next__(self): if self.value <= 500: x = self.value self.value += 100 return x else: raise StopIteration obj = IterObj()
it = iter(obj) for a in it: print(a)Output: Code: 100
200
300
400
500While using iterators, we learned that we need to implement Code: __iter__()Code: __next__()Code: StopIterationGenerators are simple functions used to create iterators and return an iterable set of items, one value at a time. You can iterate over generators only once. Let us have a look at this in a program.Example 1: Using an iterator to iterate over the values twice. Code: it = [x for x in range(6)]
print("Iterating over generator")
for i in it: print(i, end=", ")
print("\nIterating again!")
for j in it: print(j, end=", ")Output: Code: Iterating over generator
0, 1, 2, 3, 4, 5, Iterating again!
0, 1, 2, 3, 4, 5,Example 2: Using generator to iterate over values. ( The generator can be used only once, as shown in the output.) Code: gen = (x for x in range(6))
print("Iterating over generator")
for i in gen: print(i, end=", ")
print("\nTrying to Iterate over the generator again!")
for j in gen: print(j, end=", ")Output: Code: Iterating over generator
0, 1, 2, 3, 4, 5, Trying to Iterate over the generator again! Generators do not store all the values in the memory, instead, they generate the values on the fly. In the above example 2, the generator calculates and prints the value 0 and forgets it and then calculates and prints 1 and so on.Now this brings us to our discussion on the Code: yieldThe [b]yield Keyword[/b] As mentioned earlier, Code: yieldCode: returnCode: yieldExample: The following uses a generator function that yields 7 random integers between 1 and 99. Code: from random import randint def game(): # returns 6 numbers between 1 and 50 for i in range(6): yield randint(1, 50) # returns a 7th number between 51 and 99 yield randint(51,99) for random_no in game(): print("Lucky Number : ", (random_no))Output: Code: Lucky Number : 12 Lucky Number : 12 Lucky Number : 47 Lucky Number : 36 Lucky Number : 28 Lucky Number : 25 Lucky Number : 55In the above program the generator function Code: game()Code: yield Note: When the function is called, the code within the function body does not run. Instead, the function body simply returns the generator object, and then the code will continue from where it left off each time the Code: for![]() Let us discuss the workflow to make things a little simple:
Things To Remember:
[b]Comparing Time And Memory Optimization For Iterator Functions Vs Generators[/b] Example 1: The program given below computes the time and memory usage while using a function with an iterator. Code: import time
import random
import os
import psutil mobile_name = ["iPhone 11", "iPhone XR", "iPhone 11 Pro Max"]
colors = ["red","black","grey"]
def mobile_list(ph): phones = [] for i in range(ph): phone = { 'name': random.choice(mobile_name), 'color': random.choice(colors) } colors.append(phone) return phones # Calculate time of processing
t1 = time.time()
cars = mobile_list(1000000)
t2 = time.time()
print('Took {} seconds'.format(t2-t1)) # Calculate Memory used
process = psutil.Process(os.getpid())
print('Memory used: ' + str(process.memory_info().rss/1000000))output: Code: Took 14.238950252532959 seconds
Memory used: 267.157504Example 2: The following program uses a generator with the yield statement instead of a function and then we calculate the memory and time used in this case. Code: import time
import random
import os
import psutil mobile_name = ["iPhone 11", "iPhone XR", "iPhone 11 Pro Max"]
colors = ["red","black","grey"]
def mobile_list(ph): for i in range(ph): phone = { 'name': random.choice(mobile_name), 'color': random.choice(colors) } yield phone # Calculate time of processing
t1 = time.time()
for car in mobile_list(1000000): pass
t2 = time.time()
print('Took {} seconds'.format(t2-t1)) # Calculate Memory used
process = psutil.Process(os.getpid())
print('Memory used: ' + str(process.memory_info().rss/1000000))Output: Code: Took 7.272227048873901 seconds
Memory used: 15.663104The above examples clearly depict the superiority of generators and Code: yieldCode: returnDisclaimer: You have to Code: pip install psutil[b]return Keyword vs yield Keyword[/b] Before we conclude our discussion, let us finish what we started and discuss the difference between the Code: yieldCode: return![]() Conclusion In this article we learned:
Here’s a small recap of the concepts that we learned in this article; please follow the slide show give below: ![]() Please subscribeand stay tuned for more interesting articles! Where to Go From Here? Enough theory, let’s get some practice! To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs? Practice projects is how you sharpen your saw in coding! Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people? Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner. Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home. Join the free webinar now! The post Yield Keyword in Python – A Simple Illustrated Guide first appeared on Finxter. |