diff options
author | Yurii Karabas <1998uriyyo@gmail.com> | 2020-12-30 09:51:24 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-30 09:51:24 (GMT) |
commit | c56387f80c5aabf8100ceaffe365cc004ce0d7e0 (patch) | |
tree | c45546d1a311c87f58e9721a1bd232096ff07335 /Doc/howto/descriptor.rst | |
parent | ba0e49a4648e727d1a16b3ce479499eb39f22311 (diff) | |
download | cpython-c56387f80c5aabf8100ceaffe365cc004ce0d7e0.zip cpython-c56387f80c5aabf8100ceaffe365cc004ce0d7e0.tar.gz cpython-c56387f80c5aabf8100ceaffe365cc004ce0d7e0.tar.bz2 |
bpo-27794: Add `name` attribute to `property` class (GH-23967)
Diffstat (limited to 'Doc/howto/descriptor.rst')
-rw-r--r-- | Doc/howto/descriptor.rst | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/Doc/howto/descriptor.rst b/Doc/howto/descriptor.rst index ab5a573..d172c9b 100644 --- a/Doc/howto/descriptor.rst +++ b/Doc/howto/descriptor.rst @@ -934,32 +934,42 @@ here is a pure Python equivalent: if doc is None and fget is not None: doc = fget.__doc__ self.__doc__ = doc + self._name = '' + + def __set_name__(self, owner, name): + self._name = name def __get__(self, obj, objtype=None): if obj is None: return self if self.fget is None: - raise AttributeError("unreadable attribute") + raise AttributeError(f'unreadable attribute {self._name}') return self.fget(obj) def __set__(self, obj, value): if self.fset is None: - raise AttributeError("can't set attribute") + raise AttributeError(f"can't set attribute {self._name}") self.fset(obj, value) def __delete__(self, obj): if self.fdel is None: - raise AttributeError("can't delete attribute") + raise AttributeError(f"can't delete attribute {self._name}") self.fdel(obj) def getter(self, fget): - return type(self)(fget, self.fset, self.fdel, self.__doc__) + prop = type(self)(fget, self.fset, self.fdel, self.__doc__) + prop._name = self._name + return prop def setter(self, fset): - return type(self)(self.fget, fset, self.fdel, self.__doc__) + prop = type(self)(self.fget, fset, self.fdel, self.__doc__) + prop._name = self._name + return prop def deleter(self, fdel): - return type(self)(self.fget, self.fset, fdel, self.__doc__) + prop = type(self)(self.fget, self.fset, fdel, self.__doc__) + prop._name = self._name + return prop .. testcode:: :hide: |