python2與python3迭代器的使用

2021-07-22 16:45:01 字數 713 閱讀 5487

在實驗樓上學習python3的課程,在迭代器一節,怎麼輸出都不正確,錯誤提示為:

traceback (most recent call last):

file "", line 1, in next(c)

typeerror: counter object is not an iterator 、

**段如下:class counter(object):

def __init__(self, low, high):

self.current = low

self.high = high

def __iter__(self):

return self

def __next__(self):

'返回下乙個值直到當前值大於 high'

if self.current > self.high:

raise stopiteration

else:

self.current += 1

return self.current - 1

最後經過除錯發現需要將next函式的定義替換為:
def next(self):
即可正確執行。
乙個大坑啊!

Python 2 與Python 3的區別

1.除號 與整除號 python 2中,是整除 python 3中,是常規除法,是整除 2.raw input與input python 2用raw input python 3用input 都表示輸入函式。3.print與print 以及逗號 python 2中,print my print na...

Python3 與 Python2 的不同

至於學習 python3 和 python2,我了解到的觀點是這樣的。1 現在很多的專案都還是在用 python2,學習 python2 還是有意義的 2 python2 在 python 的官方已經公布了在什麼什麼時間停止維護,所以對於新手來說,學習 python2 的價值不是很大,所以直接 py...

Python2 與Python3 的區別

1.print函式 py2中print是乙個語法結構,如 print value py3中print是乙個函式,如 print value 2.除法運算 py2中兩個整數除法得到的是0,要想得到浮點數結果,則被除數或除數二者要有乙個是浮點數才行。如 print 1 4 0 print 1 4.0.2...