summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2022-10-09 02:16:10 (GMT)
committerGitHub <noreply@github.com>2022-10-09 02:16:10 (GMT)
commit820ef62833bd2d84a141adedd9a05998595d6b6d (patch)
tree16657ae20e7f67ee811b4540899af3888e2c844a /Doc
parentc86ee93d752d548550168cea68115398cc80d697 (diff)
downloadcpython-820ef62833bd2d84a141adedd9a05998595d6b6d.zip
cpython-820ef62833bd2d84a141adedd9a05998595d6b6d.tar.gz
cpython-820ef62833bd2d84a141adedd9a05998595d6b6d.tar.bz2
[3.10] Minor edits to the Descriptor HowTo Guide (GH-24901) (GH-98114)
Diffstat (limited to 'Doc')
-rw-r--r--Doc/howto/descriptor.rst14
1 files changed, 8 insertions, 6 deletions
diff --git a/Doc/howto/descriptor.rst b/Doc/howto/descriptor.rst
index 6e7719e..0c6f488 100644
--- a/Doc/howto/descriptor.rst
+++ b/Doc/howto/descriptor.rst
@@ -847,7 +847,7 @@ afterwards, :meth:`__set_name__` will need to be called manually.
ORM example
-----------
-The following code is simplified skeleton showing how data descriptors could
+The following code is a simplified skeleton showing how data descriptors could
be used to implement an `object relational mapping
<https://en.wikipedia.org/wiki/Object%E2%80%93relational_mapping>`_.
@@ -1533,6 +1533,8 @@ by member descriptors:
def __get__(self, obj, objtype=None):
'Emulate member_get() in Objects/descrobject.c'
# Also see PyMember_GetOne() in Python/structmember.c
+ if obj is None:
+ return self
value = obj._slotvalues[self.offset]
if value is null:
raise AttributeError(self.name)
@@ -1561,13 +1563,13 @@ variables:
class Type(type):
'Simulate how the type metaclass adds member objects for slots'
- def __new__(mcls, clsname, bases, mapping):
+ def __new__(mcls, clsname, bases, mapping, **kwargs):
'Emulate type_new() in Objects/typeobject.c'
# type_new() calls PyTypeReady() which calls add_methods()
slot_names = mapping.get('slot_names', [])
for offset, name in enumerate(slot_names):
mapping[name] = Member(name, clsname, offset)
- return type.__new__(mcls, clsname, bases, mapping)
+ return type.__new__(mcls, clsname, bases, mapping, **kwargs)
The :meth:`object.__new__` method takes care of creating instances that have
slots instead of an instance dictionary. Here is a rough simulation in pure
@@ -1578,7 +1580,7 @@ Python:
class Object:
'Simulate how object.__new__() allocates memory for __slots__'
- def __new__(cls, *args):
+ def __new__(cls, *args, **kwargs):
'Emulate object_new() in Objects/typeobject.c'
inst = super().__new__(cls)
if hasattr(cls, 'slot_names'):
@@ -1591,7 +1593,7 @@ Python:
cls = type(self)
if hasattr(cls, 'slot_names') and name not in cls.slot_names:
raise AttributeError(
- f'{type(self).__name__!r} object has no attribute {name!r}'
+ f'{cls.__name__!r} object has no attribute {name!r}'
)
super().__setattr__(name, value)
@@ -1600,7 +1602,7 @@ Python:
cls = type(self)
if hasattr(cls, 'slot_names') and name not in cls.slot_names:
raise AttributeError(
- f'{type(self).__name__!r} object has no attribute {name!r}'
+ f'{cls.__name__!r} object has no attribute {name!r}'
)
super().__delattr__(name)