In today's article I am going to show you how to code a Generator in which I have implemented range function and explain use of yield. how it is different from return and raising an exception.
Since In this article I am not beginning to talk from the zero its meant for those who have some knowledge of python programming.
First of All, Generators: what are Generator? A function which returns an iterator. It looks like a normal function except that values are returned to the caller using a yield statement instead of a return statement.
Yield statement: The yield statement is only used when defining a generator function, and is only used in the body of the generator function. Using a yield statement in a function definition is sufficient to cause that definition to create a generator function instead of a normal function. When a generator function is called, it returns an iterator known as a generator iterator, or more commonly, a generator. The body of the generator function is executed by calling the next() function on the generator repeatedly until it raises an exception.
Return may only occur syntactically nested in a function definition, not within a nested class definition. If an expression list is present, it is evaluated, else None is substituted. Return leaves the current function call with the expression list (or None) as return value.
When return passes control out of a try statement with a finally clause, that finally clause is executed before really leaving the function.
In a generator function, the return statement is not allowed to include an expression_list. In that context, a bare return indicates that the generator is done and will cause StopIteration to be raised.
Python's exception mechanism is the universal framework for dealing with errors—situations where your program can't really proceed normally.
The current recommended practice is to use a raise statement of this form:
raise E(...)
where E is some class derived from the built-in Exception class: you can use one of the built-in exceptions, or you can create your own exception classes.
For classes derived from Exception, the constructor takes one argument, an error message—that is, a string explaining why the exception was raised. The resulting instance makes that message available as an attribute named .message.
See example given above. I have not pasted code as text so you can practice even if you want to type the same code.
In the above example: Number of values entered should not be less than 1
or more than 3,: requires at least one argument, you entered 0
if you enter more than 3 the output will be Number of values entered should not be less than 1 or more than 3,: inclusive_range expected at most 3 arguments. you entered 4
However if you enter values either 1 or 3 arguments, it will be handled as tuple, and that tuple argument is passed to function inclusive_range() that has 4 checks out which 2 are explained above and rest of 3 are if 1 argument is passed to the function it is considered to be where the range ends, if two arguments are passed then first argument is starting number and second argument is the stop number and in the third check 3 arguments are passed & the third one is step by which it has to increment. in the previous cases, if step is not mentioned it is considered to be 1.
Now its your time to practice and if you ha