New submission from xitop <[email protected]>:
The object.__set_name__() function (introduced in Python 3.6 by PEP-487) is
mentioned in the "what's new " summary as an extension to the descriptor
protocol [1] and documented in the "implementing descriptors" section [2].
However, the PEP itself states that it "adds an __set_name__ initializer for
class attributes, especially if they are descriptors.". And it indeed works for
plain classes where the descriptor protocol is not used at all (no __get__ or
__set__ or __delete__):
----
class NotDescriptor:
def __set_name__(self, owner, name):
print('__set_name__ called')
class SomeClass:
attr = NotDescriptor()
----
It is clear that this method is helpful when used in descriptors and that is
its intended use, but other valid use-cases probably exist.
I suggest to amend the documentation to clarify that (correct me if I'm wrong)
the __set_name__ is called for every class used as an attribute in an other
class, not only for descriptors.
---
URLs:
[1]:
https://docs.python.org/3/whatsnew/3.6.html#pep-487-descriptor-protocol-enhancements
[2]: https://docs.python.org/3/reference/datamodel.html#implementing-descriptors
----------
assignee: docs@python
components: Documentation
messages: 401785
nosy: docs@python, xitop
priority: normal
severity: normal
status: open
title: __set_name__ documentation not clear about its usage with non-descriptor
classes
versions: Python 3.6, Python 3.7, Python 3.8, Python 3.9
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue45198>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com