[Python-Dev] PySequence_Check but no len (original) (raw)
Christian Tismer tismer at stackless.com
Fri Jun 22 07:17:11 EDT 2018
- Previous message (by thread): [Python-Dev] PySequence_Check but no __len__
- Next message (by thread): [Python-Dev] PySequence_Check but no __len__
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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 now sequences with undetermined or infinite length. They would be exactly those sequences which have no len attribute.
I understand now that sequences are always of fixed length and adjusted myself.
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
But the existence of len is not guaranteed or enforced. And if you look at the definition of PySequence_Fast(), you find that a sequence can be turned into a list with iteration only and no len.
So, is a sequence valid without len, if iteration is supported, instead?
There is the whole chapter about sequence protocol https://docs.python.org/3/c-api/sequence.html?highlight=sequence
but I cannot find out an exact definition what makes up a sequence?
Sorry if I'm again the only one who misunderstands the obvious :)
Best -- Chris
On 21.06.18 18:29, Brett Cannon wrote:
Sorry, I don't quite follow.
On Thu, 21 Jun 2018 at 08:50 Christian Tismer <tismer at stackless.com_ _<mailto:tismer at stackless.com>> 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 PySequenceCheck() already is true and does not care.
Which matches https://docs.python.org/3/c-api/sequence.html#c.PySequenceCheck . From Objects/abstract.c: int PySequenceCheck(PyObject *s) { if (PyDictCheck(s)) return 0; return s != NULL && s->obtype->tpassequence && s->obtype->tpassequence->sqitem != NULL; }
So if I define no len, it simply fails. Is this intended? What is "it" in this case that is failing? It isn't PySequenceCheck() so I'm not sure what the issue is. -Brett I was mislead and thought this was the unlimited case, but it seems still to be true that sequences are always finite. Can someone please enlighten me? -- Christian Tismer-Sperling :^) tismer at stackless.com <mailto:tismer at stackless.com> Software Consulting : http://www.stackless.com/ Karl-Liebknecht-Str. 121 : http://pyside.org 14482 Potsdam : GPG key -> 0xE7301150FB7BEE0E phone +49 173 24 18 776 tel:+49%20173%202418776 fax +49 (30) 700143-0023 tel:+49%2030%207001430023
Python-Dev mailing list Python-Dev at python.org <mailto:Python-Dev at python.org> https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/brett%40python.org
Python-Dev mailing list Python-Dev at python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/tismer%40stackless.com
-- Christian Tismer-Sperling :^) tismer at stackless.com Software Consulting : http://www.stackless.com/ Karl-Liebknecht-Str. 121 : http://pyside.org 14482 Potsdam : GPG key -> 0xE7301150FB7BEE0E phone +49 173 24 18 776 fax +49 (30) 700143-0023
-------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 522 bytes Desc: OpenPGP digital signature URL: <http://mail.python.org/pipermail/python-dev/attachments/20180622/f683d0fc/attachment-0001.sig>
- Previous message (by thread): [Python-Dev] PySequence_Check but no __len__
- Next message (by thread): [Python-Dev] PySequence_Check but no __len__
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]