How to create a generator/iterator with the Python C API?

后端 未结 2 1571
感动是毒
感动是毒 2020-12-12 13:45

How do I replicate the following Python code with the Python C API?

class Sequence():
    def __init__(self, max):
        self.max = max
    def data(self):
         


        
2条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-12 14:17

    In Sequence_data, you must either return a new PyInt instance or throw a StopIteration exception which tells the code outside that there are no more values. See PEP 255 for details and 9.10 Generators.

    See Iterator Protocol for helper functions in the Python/C API.

提交回复
热议问题