[Python-checkins] python/nondist/peps pep-0234.txt,1.17,1.18
ping@users.sourceforge.net
ping@users.sourceforge.net
Thu, 18 Jul 2002 13:00:25 -0700
Update of /cvsroot/python/python/nondist/peps
In directory usw-pr-cvs1:/tmp/cvs-serv19150
Modified Files:
pep-0234.txt
Log Message:
Adjusted the wording to make __iter__() more strongly required on iterators.
See http://mail.python.org/pipermail/python-dev/2002-July/026668.html .
Added text describing the distinct purposes of __iter__() and next().
See http://mail.python.org/pipermail/python-dev/2002-July/026683.html .
Index: pep-0234.txt
===================================================================
RCS file: /cvsroot/python/python/nondist/peps/pep-0234.txt,v
retrieving revision 1.17
retrieving revision 1.18
diff -C2 -d -r1.17 -r1.18
*** pep-0234.txt 8 Jul 2002 06:48:55 -0000 1.17
--- pep-0234.txt 18 Jul 2002 20:00:21 -0000 1.18
***************
*** 140,147 ****
Classes can define how they are iterated over by defining an
__iter__() method; this should take no additional arguments and
! return a valid iterator object. A class is a valid iterator
! object when it defines a next() method that behaves as described
! above. A class that wants to be an iterator also ought to
! implement __iter__() returning itself.
--- 140,163 ----
Classes can define how they are iterated over by defining an
__iter__() method; this should take no additional arguments and
! return a valid iterator object. A class that wants to be an
! iterator should implement two methods: a next() method that behaves
! as described above, and an __iter__() method that returns self.
!
! The two methods correspond to two distinct protocols:
!
! 1. An object can be iterated over with "for" if it implements
! __iter__() or __getitem__().
!
! 2. An object can function as an iterator if it implements next().
!
! Container-like objects usually support protocol 1. Iterators are
! currently required to support both protocols. The semantics of
! iteration come only from protocol 2; protocol 1 is present to make
! iterators behave like sequences. But the analogy is weak -- unlike
! ordinary sequences, iterators are "sequences" that are destroyed
! by the act of looking at their elements.
!
! Consequently, whenever any Python programmer says "for x in y",
! he or she must be sure of whether this is going to destroy y.