개발하자

다음으로 python 2.5 가져오기

Cuire 2023. 6. 11. 03:36
반응형

다음으로 python 2.5 가져오기

저는 이렇게 생긴 iter 도구에서 약간 변경된 버전의 pairwise 레시피를 사용하고 있습니다

def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = tee(iterable)
    next(b, None)
    return zip(a, b) 

이제 다음() 함수가 다음 예외를 발생시키는 코드를 실행해야 합니다:

<type 'exceptions.NameError'>: global name 'next' is not defined

파이썬 2.5에서 next()를 사용할 수 있는 방법이 있나요? 아니면 기능을 어떻게 수정해서 작동하게 해야 하나요?




이 함수에 대한 정의를 직접 쉽게 제공할 수 있습니다:

_sentinel = object()
def next(it, default=_sentinel):
    try:
        return it.next()
    except StopIteration:
        if default is _sentinel:
            raise
        return default

반응형