summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorRaymond Hettinger <rhettinger@users.noreply.github.com>2024-09-01 22:49:38 (GMT)
committerGitHub <noreply@github.com>2024-09-01 22:49:38 (GMT)
commitcb6d25011e81b46e7e6d1965dc00e4a5402c0976 (patch)
tree8ce8dfd6fcf3fc179cd6fcead677c108fcb0451a /Doc
parent91b7f2e7f6593acefda4fa860250dd87d6f849bf (diff)
downloadcpython-cb6d25011e81b46e7e6d1965dc00e4a5402c0976.zip
cpython-cb6d25011e81b46e7e6d1965dc00e4a5402c0976.tar.gz
cpython-cb6d25011e81b46e7e6d1965dc00e4a5402c0976.tar.bz2
Simplify Property() recipe to focus on the essentials (gh-123585)
Diffstat (limited to 'Doc')
-rw-r--r--Doc/howto/descriptor.rst26
1 files changed, 6 insertions, 20 deletions
diff --git a/Doc/howto/descriptor.rst b/Doc/howto/descriptor.rst
index 4b4222c..6603315 100644
--- a/Doc/howto/descriptor.rst
+++ b/Doc/howto/descriptor.rst
@@ -990,7 +990,7 @@ The documentation shows a typical use to define a managed attribute ``x``:
AttributeError: 'C' object has no attribute '_C__x'
To see how :func:`property` is implemented in terms of the descriptor protocol,
-here is a pure Python equivalent:
+here is a mostly pure Python equivalent:
.. testcode::
@@ -1004,18 +1004,10 @@ here is a pure Python equivalent:
if doc is None and fget is not None:
doc = fget.__doc__
self.__doc__ = doc
- self._name = None
+ self.__name__ = ''
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
+ self.__name__ = name
def __get__(self, obj, objtype=None):
if obj is None:
@@ -1044,19 +1036,13 @@ here is a pure Python equivalent:
self.fdel(obj)
def getter(self, fget):
- prop = type(self)(fget, self.fset, self.fdel, self.__doc__)
- prop._name = self._name
- return prop
+ return type(self)(fget, self.fset, self.fdel, self.__doc__)
def setter(self, fset):
- prop = type(self)(self.fget, fset, self.fdel, self.__doc__)
- prop._name = self._name
- return prop
+ return type(self)(self.fget, fset, self.fdel, self.__doc__)
def deleter(self, fdel):
- prop = type(self)(self.fget, self.fset, fdel, self.__doc__)
- prop._name = self._name
- return prop
+ return type(self)(self.fget, self.fset, fdel, self.__doc__)
.. testcode::
:hide: