0

Is there any way to write

a, b = generator
a, b, c = generator

and so on, meaning that the generator somehow would know how long exactly it should generate new items without presetting generator length (e.g. it is potentially infinite generator)?

martineau
  • 119,623
  • 25
  • 170
  • 301
razgibator
  • 11
  • 1
  • 1
    You could *tell* the generator how many items you'll want when you create it. Or it could be one that can create an infinite number of values. Since you've provided *no* information about the generator, you want, not much interesting to say. – Scott Hunter Mar 09 '20 at 18:16
  • 4
    Use `a, b, c = itertools.islice(generator, 3)` - see [How to slice a generator object or iterator in Python](https://stackoverflow.com/questions/34732311/how-to-slice-a-generator-object-or-iterator-in-python) – kaya3 Mar 09 '20 at 18:16
  • Thanks to everyone for your replies, but my question was about how to do it without setting actual count of upacking values. I've edited my question for more clarity. – razgibator Mar 09 '20 at 18:24
  • @razgibator that sounds like it could be functionality that requires modification of the interpreter – C.Nivs Mar 09 '20 at 18:25
  • @C.Nivs I start to think so, just asked if there was some non-obvious way to solve, since in python there are a lot of magic tricks – razgibator Mar 09 '20 at 18:30
  • No, unpacking does not support this. – juanpa.arrivillaga Mar 09 '20 at 18:31
  • @razgibator ALTERNATIVE - SOLUTUION. Actually, is not exactly the same question. I have write a solution for you problem in the following post (Search by my name): https://stackoverflow.com/questions/34732311/how-to-slice-a-generator-object-or-iterator/60607125#60607125 – Dorcioman Mar 09 '20 at 19:27

0 Answers0