Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is bug or not?
#1
Hi, I'm having a trouble with new Python 3.7 when use next code, I have got next raise Exception StopIteration, but I have not it when use 2.7.

def _zip(*args):
    iters = list(map(iter, args))
    while iters:
        res = [next(i) for i in iters]
        yield tuple(res)


if __name__ == '__main__':
    s1, s2 = 'abc', 'xyz123'
    print(list(_zip(s1, s2)))
thanks in advance.
Reply
#2
It's not a bug, the behavior was just changed in python 3.5
You can read more at https://www.python.org/dev/peps/pep-0479/
Reply
#3
I know you made this function as demonstration.
Here an example which works with Python 2.7 and Python 3.7:

from __future__ import print_function
import sys


def _zip1(*args):
    iters = list(map(iter, args))
    while iters:
        res = [next(i) for i in iters]
        yield tuple(res)

def _zip2(*args):
    for i in zip(*args):
        yield tuple(i)


if __name__ == '__main__':
    print('Python Version:', sys.version)
    s1, s2 = 'abc', 'xyz123'
    print('Your example:')
    try:
        print(list(_zip1(s1, s2)))
    except Exception as e:
        print(e)
    print('Modified version:')
    print(list(_zip2(s1, s2)))
Output:
deadeye@nexus ~ $ python w.py Python Version: 3.7.0 (default, Jun 28 2018, 17:48:59) [GCC 8.1.1 20180531] Your example: generator raised StopIteration Modified version: [('a', 'x'), ('b', 'y'), ('c', 'z')] deadeye@nexus ~ $ python2 w.py Python Version: 2.7.15 (default, Jun 27 2018, 13:05:28) [GCC 8.1.1 20180531] Your example: [('a', 'x'), ('b', 'y'), ('c', 'z')] Modified version: [('a', 'x'), ('b', 'y'), ('c', 'z')]
The modified function is even shorter and easier to understand.
Have a nice weekend.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
thanks for replies
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020