我創建一個單一的鏈接列表,返回給定範圍內的奇數,而不是返回1,3,5等,它返回1,None,3,None,5等我想使它停止返回None和只有奇數。鏈接列表返回時沒有指定不是
class Odds:
def __init__(self,end):
self.__start = 1
self.__end = end
def __iter__(self):
return OddsIterator(self.__end)
class OddsIterator:
def __init__(self,finish):
self.__current = 0
self.__step = 1
self.__end = finish
def __next__(self):
x = None
if self.__current > self.__end:
raise StopIteration
else:
self.__current += self.__step
if (self.__current - self.__step + 1) % 2 != 0:
x = self.__current - self.__step + 1
if x != None:
return x
採用雙層名稱壓延 –