Uncategorized

Python reset or reuse custom range class


Example custom range class is not reset or “reusable” like builtin range. How to make it so?

def exampleCustomRange(stopExclusive):
  for i in range(stopExclusive):
    yield i

>> builtinRange = range(3)
>> [x for x in builtinRange]
[0, 1, 2]
>> [x for x in builtinRange]
[0, 1, 2] # See how this repeats on a second try? It is reusable or reset.
>> customRange = exampleCustomRange(3)
>> [x for x in customRange]
[0, 1, 2]
>> [x for x in customRange]
[] # See how this is now empty? It is not reusable or reset.

The second use of customRange shown in repl above does not repeat, reset, or reuse like the builtin range. I want to match the behavior of the builtinRange.



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *