[Tutor] any(x)
Alan Gauld
alan.gauld at yahoo.co.uk
Mon Feb 7 18:37:44 EST 2022
On 07/02/2022 22:05, Oliver Haken wrote:
> I have a generator to make an infinite list of numbers
>
> Code: Select all<https://forums.sourcepython.com/viewtopic.php?t=2663#>
>
> t = 0
> u = []
> def h ():
> while True:
> t = t + 1
> u.append(t)
> yield u
> h ()
>
> But how do I use it?
Like any function you need to use the value returned.
Although in this case you are returning the list which
is already available as a global variable.
It would be more normal for the generator to yield the
integer and then for your code to store it in the list:
def get_num():
n = 0
while True:
n += 1
yield n
u = []
for j in range(100): # or however many numbers you need...
u.append(get_num())
print(u)
By putting the number inside the function you don't get any
weird side-effects - like if somebody modifies t between calls?
--
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
More information about the Tutor
mailing list