[Tutor] taking support of strings in solving numerical problems
Manprit Singh
manpritsinghece at gmail.com
Sun Oct 25 22:15:58 EDT 2020
Dear Sir,
Thanks for explaining.
So if fx(a, b) is a call to a generator function, where both a & b needs to
be an integer, calling fx(a, "b") will raise an error when we are actually
executing it (like doing sum(fx(a, "b"))), not at the time of assigning
this call to a variable . This is clear to me .
But still i raise objection, according to me if i am calling and then
executing ser_gen(a, b) for any integer value less than 1 , for the below
written generator function definition, it must provide an empty iterable,
the definition is given below:
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( )
Regards
Manprit Singh
if this function
On Sun, Oct 25, 2020 at 5:56 PM Alan Gauld via Tutor <tutor at python.org>
wrote:
> On 25/10/2020 08:38, Manprit Singh wrote:
>
> >>>> genfx_ser(9, "a")
> > <generator object genfx_ser at 0x7f38bf690190>
> >
> > In the second function call , see the second argument is "a", which is
> > being passed to formal parameter lim in function definition, must
> create
> > an error , since range cannot accept a non integer value, but still
> > function call is returning a generator object.
>
> This is one of the differences between a generator and a function.
> You can assign the generator to a variable then iterate over it.
> Its only when you start the iteration that the code actually gets
> executed and the error raised.
>
> So for example:
>
> >>> def f(x,n):
> nums = x
> for i in range(n):
> yield nums
> nums = nums*10 + x
>
>
> >>> f(9,3)
> <generator object f at 0x7fd53c0a39e0>
> >>> list(f(9,3))
> [9, 99, 999]
> >>> f(9,'a')
> <generator object f at 0x7fd53c0a39e0>
> >>> list(f(9,'a'))
> Traceback (most recent call last):
> File "<pyshell#58>", line 1, in <module>
> list(f(9,'a'))
> File "<pyshell#54>", line 3, in f
> for i in range(n):
> TypeError: 'str' object cannot be interpreted as an integer
> >>>
>
> You could have multiple sequences stored in variables
> and then call whichever sequence is required depending
> on user input. But they are all based on the same
> generator function.
>
> > Is it a bug ?
>
> No, its a feature! :-)
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list