[Tutor] taking support of strings in solving numerical problems

Cameron Simpson cs at cskk.id.au
Mon Oct 26 05:47:48 EDT 2020


On 26Oct2020 14:53, Manprit Singh <manpritsinghece at gmail.com> wrote:
>So from the series of mails on this topic, i have learned following facts ,
>supported with experiment given below :
>>>> def ser_gen(no, order):
>           f_no = no
>           for i in range(order):
>               yield f_no
>              f_no = f_no*10 + no
>
>>>> next(ser_gen(9, "A"))                      # execution of generator
>function call with an undesired value of second argument ("A") in function
>call
>Traceback (most recent call last):
>  File "<pyshell#8>", line 1, in <module>
>    next(ser_gen(9, "A"))
>  File "<pyshell#7>", line 3, in ser_gen
>    for i in range(order):
>TypeError: 'str' object cannot be interpreted as an integer
>>>> next(ser_gen(9, -3))                       # execution of generator
>function call with an undesired value of second argument (-3) in function
>call
>Traceback (most recent call last):
>  File "<pyshell#9>", line 1, in <module>
>    next(ser_gen(9, -3))
>StopIteration
>>>>
>
>1) In the first  execution of the function call, since "A" is string , and
>range cannot accept a string, hence Type error is generated .

Yes.

>2) In the second  execution of the function call, since -3 is negative
>integer, the for loop never got executed, The function has  returned an
>empty iterator, this is proved with the fact that next( ) has caused
>StopIteration .

Yes.

Keep in mind that the function doesn't run at all until you first call 
next(). So the calling code doesn't know the iterator is empty until it 
tries to use it. Neither does the ser_gen function - it doesn't even 
reach the start of the for loop until you first call next().

>And finally a mechanism is needed in the program to deal with these
>unexpected behaviours - like exception handling .

Yes and no.

Exceptions are just that - an indication of exceptional circumstances. 
And you'll also notice that calling next() on a generator which no 
longer has anything to yield raises StopIteration.

The rule of thumb for exceptions is to only can those you know how to 
handle - that way the unexpected get out and provide you a good stack 
trace of where things went wrong. Otherwise you just end up hiding bugs.

StopIteration is a little unusual. _Every_ generator raises StopIteration 
when you ask for the next value if there is no next value.

But the commonest way to iterate is with a for loop. Look at this:

     for f_no in ser_gen(9, 5):
         print(f_no)

This will yield 5 values, and then next time the for-loop seeks the next 
value, the generator raises StopIteration. And the for-loop expects 
that: it exits the loop when that exception is raised. The logic is like 
this:

    g = ser_gen(9, 5)   # returns the generator
    while True:
        try:
            f_no = next(g)
        except StopIteration:
            break
        print(f_no)

but more readable.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Tutor mailing list