[Tutor] taking support of strings in solving numerical problems

dn PyTutor at DancesWithMice.info
Mon Oct 26 06:09:57 EDT 2020


On 26/10/2020 22:35, Cameron Simpson wrote:
> On 26Oct2020 08:06, Alan Gauld <alan.gauld at yahoo.co.uk> wrote:
>> On 26/10/2020 04:55, Cameron Simpson wrote:
>>>> def ser_gen(no, order):
>>>>     f_no = no
>>>>     for i in range(order):
>>>>         yield f_no
>>>>         f_no = f_no*10 + no
>>>>
>>>> all(ser_gen(9, -3))     Returns True that says, there is an empty iterable
>>>> inside all( )
>>>
>>> Returns True for me, too. Which is expected behaviour.
>>>
>>> It is not clear to me what your objection is about.
>>
>> To be honest it surprised me too. I expected the function to fall
>> off the bottom and return none. But in fact it raises a
>> StopIteration instead, which all() interprets as an empty sequence.
> 
> It didn't surprise me. It is just end of execution of a generator function.
> This is why I couldn't see what Manprit is concerned about.


Obtaining same result to original code, but is the generator called 
correctly?

 >>> def ser_gen(no, order):
...     f_no = no
...     for i in range(order):
...         yield f_no
...         f_no = f_no*10 + no
...
 >>> all(ser_gen(9, -3)) 

True


### What is really going-on?
### Simplifying:

 >>> print( ser_gen( 9, -3 ) )
<generator object ser_gen at 0x7fd5cf446350>

### Even if the arguments are acceptable:

 >>> print(ser_gen(9, 3))
<generator object ser_gen at 0x7fd5cf446350>

Is the presence of an object 'truthy', hence the all() result?
-- 
Regards =dn


More information about the Tutor mailing list