Re: [Python-Dev] PySequence_Check but no __len__

2018-06-22 Thread Terry Reedy
On 6/22/2018 7:57 PM, Greg Ewing wrote: Terry Reedy wrote: I am surprised that a C-API function calls something a 'sequence' without it having __len__. It's a bit strange that PySequence_Check exists at all. The principle of duck typing would suggest that one should be checking for the specifi

Re: [Python-Dev] PySequence_Check but no __len__

2018-06-22 Thread Greg Ewing
Ivan Pozdeev via Python-Dev wrote: the documentation seems to use "sequence" in the sense "finite iterable". Functions that need to know the length of input in advance seem to be the minority. The official classifications we have are: Sequence: __iter__, __getitem__, __len__ Iterable: __iter

Re: [Python-Dev] PySequence_Check but no __len__

2018-06-22 Thread Greg Ewing
Terry Reedy wrote: I am surprised that a C-API function calls something a 'sequence' without it having __len__. It's a bit strange that PySequence_Check exists at all. The principle of duck typing would suggest that one should be checking for the specific methods one needs. I suspect it's a ho

Re: [Python-Dev] PySequence_Check but no __len__

2018-06-22 Thread Paul Moore
On 22 June 2018 at 20:17, Ivan Pozdeev via Python-Dev wrote: > On 22.06.2018 22:07, Terry Reedy wrote: >> https://docs.python.org/3/library/collections.abc.html#collections-abstract-base-classes >> >> says that a Sequence has both __getitem__ and __len__. >> >> I am surprised that a C-API functio

Re: [Python-Dev] PySequence_Check but no __len__

2018-06-22 Thread Ivan Pozdeev via Python-Dev
On 22.06.2018 22:17, Ivan Pozdeev wrote: On 22.06.2018 22:07, Terry Reedy wrote: On 6/22/2018 7:17 AM, Christian Tismer wrote: My problem is to find out how to deal with a class which has __getitem__ but no __len__. The documentation suggests that the length of a sequence can always be obtai

Re: [Python-Dev] PySequence_Check but no __len__

2018-06-22 Thread Ivan Pozdeev via Python-Dev
On 22.06.2018 22:07, Terry Reedy wrote: On 6/22/2018 7:17 AM, Christian Tismer wrote: My problem is to find out how to deal with a class which has __getitem__ but no __len__. The documentation suggests that the length of a sequence can always be obtained by len(). https://docs.python.org/3/re

Re: [Python-Dev] PySequence_Check but no __len__

2018-06-22 Thread Terry Reedy
On 6/22/2018 7:17 AM, Christian Tismer wrote: My problem is to find out how to deal with a class which has __getitem__ but no __len__. The documentation suggests that the length of a sequence can always be obtained by len(). https://docs.python.org/3/reference/datamodel.html It says that pla

Re: [Python-Dev] PySequence_Check but no __len__

2018-06-22 Thread Nick Coghlan
On 22 June 2018 at 21:45, Christian Tismer wrote: > Answering myself: > > PySequence_Check determines a sequence. See the docs. > > len() can but does not have to exist. > The size is always limited. Just to throw a couple of extra wrinkles on this: Due to a C API implementation detail in CPytho

Re: [Python-Dev] PySequence_Check but no __len__

2018-06-22 Thread Christian Tismer
Answering myself: PySequence_Check determines a sequence. See the docs. len() can but does not have to exist. The size is always limited. After evicting my initial fault, this is now obvious. Sorry about the noise. On 22.06.18 13:17, Christian Tismer wrote: > Hi Brett, > > because you did not

Re: [Python-Dev] PySequence_Check but no __len__

2018-06-22 Thread Christian Tismer
Hi Brett, because you did not understand me, I must have had a fundamental misunderstanding. So I started a self-analysis and came to the conclusion that this was my error since maybe a decade: When iterators and generators came into existence, I somehow fell into the trap to think that there are

Re: [Python-Dev] PySequence_Check but no __len__

2018-06-21 Thread Brett Cannon
Sorry, I don't quite follow. On Thu, 21 Jun 2018 at 08:50 Christian Tismer wrote: > Hi friends, > > there is a case in the Python API where I am not sure what to do: > > If an object defines __getitem__() only but no __len__(), > then PySequence_Check() already is true and does not care. > Whic

[Python-Dev] PySequence_Check but no __len__

2018-06-21 Thread Christian Tismer
Hi friends, there is a case in the Python API where I am not sure what to do: If an object defines __getitem__() only but no __len__(), then PySequence_Check() already is true and does not care. So if I define no __len__, it simply fails. Is this intended? I was mislead and thought this was the