++ (see here) 나는 다음과 같은 파이썬 기능 iterUniqueCombos()
와 동일하게 수행하는 파이썬 모듈 제공 할 계획 :Python iterator 객체를 반환 할 수있는 (파이썬 모듈의) C 코드를 작성하는 방법은 무엇입니까? 내가 곧 C에서 최소한의 Python3.6 확장 모듈을 서면으로 성공 후
def iterUniqueCombos(lstOfSortableItems, sizeOfCombo):
lstOfSortedItems = sorted(lstOfSortableItems)
sizeOfList = len(lstOfSortedItems)
lstComboCandidate = []
def idxNextUnique(idxItemOfList):
idxNextUniqueCandidate = idxItemOfList + 1
while (
idxNextUniqueCandidate < sizeOfList
and
lstOfSortedItems[idxNextUniqueCandidate] == lstOfSortedItems[idxItemOfList]
): # while
idxNextUniqueCandidate += 1
idxNextUnique = idxNextUniqueCandidate
return idxNextUnique
def combinate(idxItemOfList):
if len(lstComboCandidate) == sizeOfCombo:
yield tuple(lstComboCandidate)
elif sizeOfList - idxItemOfList >= sizeOfCombo - len(lstComboCandidate):
lstComboCandidate.append(lstOfSortedItems[idxItemOfList])
yield from combinate(idxItemOfList + 1)
lstComboCandidate.pop()
yield from combinate(idxNextUnique(idxItemOfList))
yield from combinate(0)
나는 몇 가지 기본이를 파이썬과 C++ 프로그래밍에 대한 이해는 있지만, 파이썬 (Pythons) 을으로 변환하는 방법은 파이썬 확장 모듈의 C++ 코드로 절대 이해할 수 없습니다. 내 질문은 :
How to write C++ code (of a Python module) able to return a Python iterator object?
나를 시작하는 데 도움이되는 모든 의견을 환영합니다.
UPDATE (상태 2017년 5월 7일) :
주석 둘 : 수율이 더 C++ 해당이 없습니다. 필자는 Python에서 반복자 프로토콜을 수동으로 구현하여 마인드에서 수익률과 수익률을 극복하기 시작했다. - user2357112 Apr 26 at 1:16 그리고 대답의 힌트는 danny
입니다.이 질문에 대한 대답은 'yield를 사용하지 않고 반복자를 구현하는 방법은 무엇인가'와 같지만 순수 Python 대신 C++ 확장을 묻는 것과 같습니다.은 yield
을 제거하고 Python 확장 모듈의 C 코드를 처음부터 작성하여 (Segmentation Fault
오류가 발생 했음) 알고리즘 코드를 다시 작성하여 휠을 다시 작성하는 잘못된 방향으로 프로그래밍 작업을했습니다.
The state-of-the-art of my current knowledge on the subject of the question is that using Cython it is possible to translate the above Python code (which is using
yield
) directly into C code of a Python extension module.
이것은 (아무것도 재 작성 필요없이)는 그대로 파이썬 코드를 사용하지 않는 경우에만 가능하지만,에 yield
실행을 사용하여 알고리즘에서 사이 썬에 의해 생성 된 확장 모듈의 속도에 추가 __iter__
및 __next__
다시 작성 알고리즘 (Cython 특정 속도 최적화 코드가 Python 스크립트에 추가되지 않은 경우 후자는 유효 함)을 사용하여 반복 모듈에 생성 된 확장 모듈의 최소 두 배 이상.
'yield'에는 C++가 없습니다. 먼저 [iterator 프로토콜을 수동으로 구현] (https : //www.python.org/dev/peps/pep-0234 /)를 파이썬에서 사용하면 마인드 세트에서 'yield'와 'yield'를 벗어날 수 있습니다. – user2357112