Skip to content

Commit 2d2e01a

Browse files
geryogamrhettinger
andauthored
Minor edits to the Descriptor HowTo Guide (GH-24901)
Co-authored-by: Raymond Hettinger <[email protected]>
1 parent 3378ebb commit 2d2e01a

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

Doc/howto/descriptor.rst

+8-6
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ afterwards, :meth:`__set_name__` will need to be called manually.
847847
ORM example
848848
-----------
849849

850-
The following code is simplified skeleton showing how data descriptors could
850+
The following code is a simplified skeleton showing how data descriptors could
851851
be used to implement an `object relational mapping
852852
<https://en.wikipedia.org/wiki/Object%E2%80%93relational_mapping>`_.
853853

@@ -1535,6 +1535,8 @@ by member descriptors:
15351535
def __get__(self, obj, objtype=None):
15361536
'Emulate member_get() in Objects/descrobject.c'
15371537
# Also see PyMember_GetOne() in Python/structmember.c
1538+
if obj is None:
1539+
return self
15381540
value = obj._slotvalues[self.offset]
15391541
if value is null:
15401542
raise AttributeError(self.name)
@@ -1563,13 +1565,13 @@ variables:
15631565
class Type(type):
15641566
'Simulate how the type metaclass adds member objects for slots'
15651567

1566-
def __new__(mcls, clsname, bases, mapping):
1568+
def __new__(mcls, clsname, bases, mapping, **kwargs):
15671569
'Emulate type_new() in Objects/typeobject.c'
15681570
# type_new() calls PyTypeReady() which calls add_methods()
15691571
slot_names = mapping.get('slot_names', [])
15701572
for offset, name in enumerate(slot_names):
15711573
mapping[name] = Member(name, clsname, offset)
1572-
return type.__new__(mcls, clsname, bases, mapping)
1574+
return type.__new__(mcls, clsname, bases, mapping, **kwargs)
15731575
15741576
The :meth:`object.__new__` method takes care of creating instances that have
15751577
slots instead of an instance dictionary. Here is a rough simulation in pure
@@ -1580,7 +1582,7 @@ Python:
15801582
class Object:
15811583
'Simulate how object.__new__() allocates memory for __slots__'
15821584

1583-
def __new__(cls, *args):
1585+
def __new__(cls, *args, **kwargs):
15841586
'Emulate object_new() in Objects/typeobject.c'
15851587
inst = super().__new__(cls)
15861588
if hasattr(cls, 'slot_names'):
@@ -1593,7 +1595,7 @@ Python:
15931595
cls = type(self)
15941596
if hasattr(cls, 'slot_names') and name not in cls.slot_names:
15951597
raise AttributeError(
1596-
f'{type(self).__name__!r} object has no attribute {name!r}'
1598+
f'{cls.__name__!r} object has no attribute {name!r}'
15971599
)
15981600
super().__setattr__(name, value)
15991601

@@ -1602,7 +1604,7 @@ Python:
16021604
cls = type(self)
16031605
if hasattr(cls, 'slot_names') and name not in cls.slot_names:
16041606
raise AttributeError(
1605-
f'{type(self).__name__!r} object has no attribute {name!r}'
1607+
f'{cls.__name__!r} object has no attribute {name!r}'
16061608
)
16071609
super().__delattr__(name)
16081610

0 commit comments

Comments
 (0)