簡體   English   中英

Python:為什么不將“next()”稱為“__next __()”

[英]Python: Why isn't “next()” called “__next__()”

我正在使用Python 2.7。

按照慣例,內置方法如__iter____init__ __str__ __init____str__ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 為什么next()函數沒有這個?

它與其他內置函數一起使用: https//docs.python.org/2/library/functions.html ,這似乎不是一個非常一致的約定。

內置函數沒有__因為沒有內置函數。 您使用特殊方法(如__str__ )混淆了內置__str__ (如str )。

現在,內置next調用的特殊方法在Python 2.7中也被稱為next ,但該命名被認為是一個意外,並在Python 3中更改為__next__

它是在Python v3.x中。 並且仍然存在向后兼容性。 來自http://docs.pythonsprints.com/python3_porting/py-porting.html

Python 3引入了next()函數來替換迭代器對象上的next()方法。 而不是在迭代器上調用方法,而是使用iterable對象作為其唯一參數調用next()函數,該參數調用底層的__next__()方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM