Defining and Using Coroutines in Python: A Complete Guide
1. Defining a Coroutine in Python
• Define a coroutine function 'linear_equation' which takes two arguments 'a' and 'b'.
• Any coroutine derived from 'linear_equation' should be capable of taking a number as input, and evaluating the expression 'a*(x**2)+ b'.
• The couroutine after evaluating the expression should print the message 'Expression, 3*x^2+4, with x being 6 equals 112'.
Hint: Use print() instead of 'return', to print the output.
-----------
Solutions
#!/bin/python3
import sys
# Define the coroutine function 'linear_equation' below.
def linear_equation(a, b):
while True:
x=yield
c=a*(pow(x,2))+b
print('Expression, '+str(a)+'*x^2 + '+str(b)+', with x being '+str(x)+ ' equals '+str(c))
if __name__ == "__main__":
a = float(input())
b = float(input())
equation1 = linear_equation(a, b)
next(equation1)
equation1.send(6)
2. Give a Try - Define a Decorator for Coroutine
• Define a Decorator 'coroutine_decorator', which can decorate any coroutine function.
• The decorator must create the coroutine, call 'next' on it and return the coroutine that is ready for accepting any input.
• For e.g
@coroutine_decorator def linear_equation(a,b):
e1 = linear_equation(3, 4) # e1 should able to accept input without calling 'next' on it. e1.send(6)
-----------
Solution
import sys
import os
# Define 'coroutine_decorator' below
def coroutine_decorator(coroutine_func):
def wrapper(*args, **kwdargs):
c = coroutine_func(*args, **kwdargs)
next(c)
return c
return wrapper
# Define coroutine 'linear_equation' as specified in previous exercise
@coroutine_decorator
def linear_equation(a, b):
while True:
x=yield
c=a*(pow(x,2))+b
print('Expression, '+str(a)+'*x^2 + '+str(b)+', with x being '+str(x)+ ' equals '+str(c))
if __name__ == "__main__":
a = float(input())
b = float(input())
equation1 = linear_equation(a, b)
equation1.send(6)
3. Give a Try - Linking two coroutines
• Define 'linear_equation' and 'coroutine_decorator' functions as requested in previous test case.
• Define a coroutine function 'numberParser', which is capable of converting the passed input into an integer and also sends the integers to two linear equation coroutines equation1 and 'equation2`.
• 'equation1' represents linear equation coroutine with a = 3 and b = 4
'equation2' represents linear equation coroutine with a = 2 and b = -1
------------
#!/bin/python3
import sys
# Define the function 'coroutine_decorator' below
def coroutine_decorator(coroutine_func):
def wrapper(*args, **kwdargs):
c = coroutine_func(*args, **kwdargs)
next(c)
return c
return wrapper
# Define the coroutine function 'linear_equation' below
@coroutine_decorator
def linear_equation(a, b):
while True:
x=yield
c=a*(pow(x,2))+b
print('Expression, '+str(a)+'*x^2 + '+str(b)+', with x being '+str(x)+ ' equals '+str(c))
# Define the coroutine function 'numberParser' below
@coroutine_decorator
def numberParser():
equation1 = linear_equation(3, 4)
equation2 = linear_equation(2, -1)
# code to send the input number to both the linear equations
while True:
m=yield
equation1.send(m)
equation2.send(m)
def main(x):
n = numberParser()
n.send(x)
if __name__ == "__main__":
x = float(input())
res = main(x);