On 3/18/2022 10:01 AM, Ronald Oussoren via Python-Dev wrote:
On 18 Mar 2022, at 14:37, Joao S. O. Bueno <jsbueno@python.org.br> wrote:
IMO this is a purism that have little, if any place in language restrictions. I see that not allowing. "run once" iterables could indeed void attempts to write "deliberatly non cooperative code" - but can it even be reliably detected?
The other changes seem just to break backwards compatibility for little or no gain at all.
There a practical need for these changes. See https://bugs.python.org/issue46382 for a case where dataclasses needs __slots__ to be iterated over after the class has been created. And it would be good if __slots__ accurately reflected the slots that were actually created.
It may not be worth the trouble to fix this, but Serhiy’s proposal does try to fix a ward.
It may be better to rely on linter’s here, but one way to do this with few backward compatibility concerns:
- if __slots__ is a dict keep it as is - Otherwise use tuple(__slots__) while constructing the class and store that value in the __slots__ attribute of the class
That way the value of the attribute reflects the slots that were created while not breaking code that uses __slots__ and doesn’t change the value after class creation.
I like this approach, too. Eric
Ronald
On Fri, Mar 18, 2022 at 6:57 AM Ronald Oussoren via Python-Dev <python-dev@python.org> wrote:
On 18 Mar 2022, at 10:29, Serhiy Storchaka <storchaka@gmail.com> wrote:
Currently __slots__ can be either string or an iterable of strings.
1. If it is a string, it is a name of a single slot. Third-party code which iterates __slots__ will be confused.
2. If it is an iterable, it should emit names of slots. Note that non-reiterable iterators are accepted too, but it causes weird bugs if __slots__ is iterated more than once. For example it breaks default pickling and copying.
I propose to restrict the type of __slots__. Require it always been a tuple of strings. Most __slots__ in real code are tuples. It is rarely we need only single slot and set __slots__ as a string.
It will break some code (there are 2 occurrences in the stdlib an 1 in scripts), but that code can be easily fixed.
Pydoc supports __slots__ that is a dict, and will use the values in the dict als documentation for the slots. I’ve also seen code using ``__slots__ = “field1 field2”.split()``. I don’t particularly like this code pattern, but your proposal would break this.
Also note that __slots__ only has a side effect during class definition, changing it afterwards is possible but has no effect (“class Foo: pass; Foo.__slots__ = 42”). This surprised my recently and I have no idea if this feature is ever used.
Ronald
_______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-leave@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/E32BRLAW... Code of Conduct: http://python.org/psf/codeofconduct/
—
Twitter / micro.blog: @ronaldoussoren Blog: https://blog.ronaldoussoren.net/
_______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-leave@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/YQUWR7CY... Code of Conduct: http://python.org/psf/codeofconduct/
—
Twitter / micro.blog: @ronaldoussoren Blog: https://blog.ronaldoussoren.net/
_______________________________________________ Python-Dev mailing list --python-dev@python.org To unsubscribe send an email topython-dev-leave@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived athttps://mail.python.org/archives/list/python-dev@python.org/message/FZFRSHSJ... Code of Conduct:http://python.org/psf/codeofconduct/