Message401785
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 |
|
Date |
User |
Action |
Args |
2021-09-14 16:40:25 | xitop | set | recipients:
+ xitop, docs@python |
2021-09-14 16:40:25 | xitop | set | messageid: <1631637625.07.0.44020466506.issue45198@roundup.psfhosted.org> |
2021-09-14 16:40:25 | xitop | link | issue45198 messages |
2021-09-14 16:40:24 | xitop | create | |
|