Infinate Number Generator - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: General (https://python-forum.io/forum-1.html) +--- Forum: Code sharing (https://python-forum.io/forum-5.html) +--- Thread: Infinate Number Generator (/thread-12895.html) |
Infinate Number Generator - Larz60+ - Sep-18-2018 There's probably already something like this in collections, but I wanted to write one, so came up with this. I found it useful for a test case. to use, instantiate class NumberGenerator like myctr = NumberGenerator(12, 8).numgen now each time you call myctr() it returns a number starting at 12, and increments by 8 of course you can do the sane thing with range, if in a loop, this one does not require the loop. class NumberGenerator: def __init__(self, start=0, increment=1): self.start = start self.increment = increment self.current_num = start def numgen(self): retval = self.current_num self.current_num += self.increment return retval def testit(): # test1 create a counter1 counts starting at 15 incrementing by 2 c1 = NumberGenerator(15, 2).numgen for x in range(10): print(f'c1: {c1()}') print() # test2 create another counter2 starts at 25 decrementing by -3 c2 = NumberGenerator(25, -3).numgen for x in range(10): print(f'c2: {c2()}') # test 3 one more time for each counter print(f'\nc1 again: {c1()}') print(f'c2 again: {c2()}\n') if __name__ == '__main__': testit()output:
RE: Infinate Number Generator - micseydel - Sep-18-2018 This definitely be done with a built-in
RE: Infinate Number Generator - wavic - Sep-18-2018 How about this: >>> numgen = lambda start, stop, step: (num for num in range(start, stop, step)) >>> for n in numgen(10, 0, -2): ... print(n) ... 10 8 6 4 2 >>> RE: Infinate Number Generator - micseydel - Sep-18-2018 (Sep-18-2018, 03:53 AM)wavic Wrote: How about this:Can't you just inline the range() call there? The lambda and comprehension are just indirection wrapping it. RE: Infinate Number Generator - wavic - Sep-18-2018 It's not a comprehension but generator expression. I am using lambda because range requires at least one parameter and I can't pass it just using an expression alone. Like: (n for n in range())
RE: Infinate Number Generator - micseydel - Sep-18-2018 Comprehensions can be of the list or generator variety (or for that matter sets and dictionaries in modern Python). Let me show you what I meant: It produces the same output as yours, minus the comprehension, minus the lambda. I don't understand what benefit there is to including them when you can skip them without any issues.EDIT: to clarify, the link I have here says that the original docs didn't say "generate comprehension" but calls for people to use the term. I don't want to dig into this particular terminology argument, but I think it's a reasonable term, I think Ned is a good Python authority, and the rest of my points about indirection still stand. RE: Infinate Number Generator - wavic - Sep-18-2018 Yes, this implementation is totally useless because range produces one value at a time. |