summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorEugene Toder <eltoder@users.noreply.github.com>2024-02-20 15:14:34 (GMT)
committerGitHub <noreply@github.com>2024-02-20 15:14:34 (GMT)
commitc0b0c2f2015fb27db4306109b2b3781eb2057c2b (patch)
tree49b34e2f7d762b0abeb3c09e508c8e2fd9255c08 /Doc
parent9af80ec83d1647a472331bd1333a7fa9108fe98e (diff)
downloadcpython-c0b0c2f2015fb27db4306109b2b3781eb2057c2b.zip
cpython-c0b0c2f2015fb27db4306109b2b3781eb2057c2b.tar.gz
cpython-c0b0c2f2015fb27db4306109b2b3781eb2057c2b.tar.bz2
gh-101860: Expose __name__ on property (GH-101876)
Useful for introspection and consistent with functions and other descriptors.
Diffstat (limited to 'Doc')
-rw-r--r--Doc/howto/descriptor.rst19
1 files changed, 15 insertions, 4 deletions
diff --git a/Doc/howto/descriptor.rst b/Doc/howto/descriptor.rst
index e72386a..51f9f4a 100644
--- a/Doc/howto/descriptor.rst
+++ b/Doc/howto/descriptor.rst
@@ -1004,31 +1004,42 @@ here is a pure Python equivalent:
if doc is None and fget is not None:
doc = fget.__doc__
self.__doc__ = doc
- self._name = ''
+ self._name = None
def __set_name__(self, owner, name):
self._name = name
+ @property
+ def __name__(self):
+ return self._name if self._name is not None else self.fget.__name__
+
+ @__name__.setter
+ def __name__(self, value):
+ self._name = value
+
def __get__(self, obj, objtype=None):
if obj is None:
return self
if self.fget is None:
raise AttributeError(
- f'property {self._name!r} of {type(obj).__name__!r} object has no getter'
+ f'property {self.__name__!r} of {type(obj).__name__!r} '
+ 'object has no getter'
)
return self.fget(obj)
def __set__(self, obj, value):
if self.fset is None:
raise AttributeError(
- f'property {self._name!r} of {type(obj).__name__!r} object has no setter'
+ f'property {self.__name__!r} of {type(obj).__name__!r} '
+ 'object has no setter'
)
self.fset(obj, value)
def __delete__(self, obj):
if self.fdel is None:
raise AttributeError(
- f'property {self._name!r} of {type(obj).__name__!r} object has no deleter'
+ f'property {self.__name__!r} of {type(obj).__name__!r} '
+ 'object has no deleter'
)
self.fdel(obj)