Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 33.9k
Minor edits to the Descriptor HowTo Guide#24901
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
58b799df77b0f91dbd876d0d301ee87fe3752a06d02f9971b2f335ca4886dbb705c577c0e643281ec93c34eef3aae8c6221d5fb964671b910a132bf62fe4fc1252f4b58ae9775630c64bfb33f75d45eaac853e6bbcc9da0File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
geryogam marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| be used to implement an `object relational mapping | ||
| <https://en.wikipedia.org/wiki/Object%E2%80%93relational_mapping>`_. | ||
| @@ -1535,6 +1535,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 | ||
geryogam marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| value = obj._slotvalues[self.offset] | ||
| if value is null: | ||
| raise AttributeError(self.name) | ||
| @@ -1563,13 +1565,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 | ||
| @@ -1580,7 +1582,7 @@ Python: | ||
| class Object: | ||
| 'Simulate how object.__new__() allocates memory for __slots__' | ||
| def __new__(cls, *args): | ||
| def __new__(cls, *args, **kwargs): | ||
geryogam marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page.
geryogam marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| 'Emulate object_new() in Objects/typeobject.c' | ||
| inst = super().__new__(cls) | ||
| if hasattr(cls, 'slot_names'): | ||
| @@ -1593,7 +1595,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}' | ||
geryogam marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page.
geryogam marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| ) | ||
| super().__setattr__(name, value) | ||
| @@ -1602,7 +1604,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}' | ||
geryogam marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page.
geryogam marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| ) | ||
| super().__delattr__(name) | ||
Uh oh!
There was an error while loading. Please reload this page.