0

Here's what's happening on my NodeMCU board with ESP8266:

>>> x = iter((28,75,127,179))
>>> x.next()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'iterator' object has no attribute 'next'

Same occurs with a custom defined generator:

>>> def foo():
...     for i in (28,75,127,179):
...         yield i
...         
...         
... 
>>> foo
<generator>
>>> f = foo()
>>> f.next()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'generator' object has no attribute 'next'

Seemingly this would work, since objects are indeed recognized as generators/iterators. Question is then, how do I go about making this work ?

Sergiy Kolodyazhnyy
  • 938
  • 1
  • 13
  • 41

1 Answers1

3

So apparently, MicroPython implements iterators in Python 3 style since MicroPython is Python 3 implementation , rather than Python 2. What I was doing in my question is basically straight from Python 2 tutorial. However, in Python 3 way of things this works:

>>> def foo():
...     while True:
...         for i in (28,75,127,179):
...             yield i
...             
...             
... 
>>> f = foo()
>>> next(f)
28
>>> next(f)
75
>>> next(f)
127
>>> next(f)
179
>>> next(f)
28
>>> next(f)
75
Sergiy Kolodyazhnyy
  • 938
  • 1
  • 13
  • 41
  • 2
    Calling `f.__next__()` is the Python3 equivalent to `f.next()`. `next(f)` is the preferred way as it works in Python2 and Python3 – John La Rooy Oct 08 '18 at 04:15
  • 2
    [*MicroPython is a lean and efficient implementation of the Python 3 programming language*](https://micropython.org/), so I would always expect Python 3 behaviour. – nekomatic Oct 08 '18 at 10:20