Skip to main content
added 149 characters in body
Source Link
lennon310
  • 3.2k
  • 7
  • 19
  • 35

PEP 234 says

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:

  • An object can be iterated over with for if it implements iter() or getitem().

  • An object can function as an iterator if it implements next().

This looks reasonable because one can do forward seek with x times of next() operations, and the iterator state in this case is not expected to be changed by a random access operation. So 1a and 2a seems to be ruled out.

2b:

__getitem__ and next() are implemented complete independently, i.e., the index increment in next() should not affect the index used in getter in the iter object. On the other hand, however, it also means the API should be separated to two -- one for iterator another for indexing. Or even no need to implement indexing at all --- you can use "abcdefghij"[1] for index purpose already.

1b:

This does not violate the independent-protocols mentioned above, I'm interested in the specific use case of such an indexable iterator class though. Also the case -- when next() is called several times and the pos reaches to the end, then __getitem__ is called -- needs to be handled too.

PEP 234 says

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:

  • An object can be iterated over with for if it implements iter() or getitem().

  • An object can function as an iterator if it implements next().

This looks reasonable because one can do forward seek with x times of next operations, and the iterator state in this case is not expected to be changed by a random access operation. So 1a and 2a seems to be ruled out.

2b:

__getitem__ and next are implemented complete independently, i.e., the index increment in next() should not affect the index used in getter in the iter object. On the other hand, however, it also means the API should be separated to two -- one for iterator another for indexing. Or even no need to implement indexing at all --- you can use "abcdefghij"[1] for index purpose already.

1b:

This does not violate the independent-protocols mentioned above, I'm interested in the specific use case of such an indexable iterator class though.

PEP 234 says

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:

  • An object can be iterated over with for if it implements iter() or getitem().

  • An object can function as an iterator if it implements next().

This looks reasonable because one can do forward seek with x times of next() operations, and the iterator state in this case is not expected to be changed by a random access operation. So 1a and 2a seems to be ruled out.

2b:

__getitem__ and next() are implemented complete independently, i.e., the index increment in next() should not affect the index used in getter in the iter object. On the other hand, however, it also means the API should be separated to two -- one for iterator another for indexing. Or even no need to implement indexing at all --- you can use "abcdefghij"[1] for index purpose already.

1b:

This does not violate the independent-protocols mentioned above, I'm interested in the specific use case of such an indexable iterator class though. Also the case -- when next() is called several times and the pos reaches to the end, then __getitem__ is called -- needs to be handled too.

added 419 characters in body
Source Link
lennon310
  • 3.2k
  • 7
  • 19
  • 35

PEP 234 says

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:

  • An object can be iterated over with for if it implements iter() or getitem().

  • An object can function as an iterator if it implements next().

So my intuition isThis looks reasonable because one can do forward seek with x times of next operations, and the iterator state in this case is not expected to be changed by a random access operation. So 1a and 2a seems to be ruled out.

2b:

__getitem__ and next should beare implemented complete independently (option 2b), i.e., the index increment in next() should not affect the index used in getter in the iter object. On the other hand, however, it also means the API should be separated to two -- one for iterator another for indexing. Or even no need to implement indexing at all --- you can use "abcdefghij"[1] for index purpose already.

1b:

This does not violate the independent-protocols mentioned above, I'm interested in the specific use case of such an indexable iterator class though, since it may determine how getitem and next are implemented too.

PEP 234 says

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:

  • An object can be iterated over with for if it implements iter() or getitem().

  • An object can function as an iterator if it implements next().

So my intuition is __getitem__ and next should be implemented independently (option 2b), i.e., the index increment in next() should not affect the index used in getter in the iter object.

I'm interested in the specific use case of such an indexable iterator class though, since it may determine how getitem and next are implemented too.

PEP 234 says

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:

  • An object can be iterated over with for if it implements iter() or getitem().

  • An object can function as an iterator if it implements next().

This looks reasonable because one can do forward seek with x times of next operations, and the iterator state in this case is not expected to be changed by a random access operation. So 1a and 2a seems to be ruled out.

2b:

__getitem__ and next are implemented complete independently, i.e., the index increment in next() should not affect the index used in getter in the iter object. On the other hand, however, it also means the API should be separated to two -- one for iterator another for indexing. Or even no need to implement indexing at all --- you can use "abcdefghij"[1] for index purpose already.

1b:

This does not violate the independent-protocols mentioned above, I'm interested in the specific use case of such an indexable iterator class though.

added 156 characters in body
Source Link
lennon310
  • 3.2k
  • 7
  • 19
  • 35

PEP 234 says

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:

  • An object can be iterated over with for if it implements iter() or getitem().

  • An object can function as an iterator if it implements next().

So my intuition is __getitem__ and next should be implemented independently (option 2b), i.e., the index increment in next() should not affect the index used in getter in the iter object.

I'm interested in the specific use case of such an indexable iterator class though, since it may determine how getitem and next are implemented too.

PEP 234 says

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:

  • An object can be iterated over with for if it implements iter() or getitem().

  • An object can function as an iterator if it implements next().

So my intuition is __getitem__ and next should be implemented independently (option 2b), i.e., the index increment in next() should not affect the index used in getter in the iter object.

PEP 234 says

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:

  • An object can be iterated over with for if it implements iter() or getitem().

  • An object can function as an iterator if it implements next().

So my intuition is __getitem__ and next should be implemented independently (option 2b), i.e., the index increment in next() should not affect the index used in getter in the iter object.

I'm interested in the specific use case of such an indexable iterator class though, since it may determine how getitem and next are implemented too.

Source Link
lennon310
  • 3.2k
  • 7
  • 19
  • 35
Loading