Skip to content

Commit ed8c1a4

Browse files
committed
Object model: __set_name__, __objclass__, __name__
Signed-off-by: Alexey Stepanov <penguinolog@gmail.com>
1 parent d8ec23a commit ed8c1a4

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

advanced_descriptors/separate_class_method.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class SeparateClassMethod:
8989
True
9090
"""
9191

92-
__slots__ = ("__instance_method", "__class_method")
92+
__slots__ = ("__instance_method", "__class_method", "__owner", "__name")
9393

9494
def __init__(
9595
self, imeth: typing.Optional[typing.Callable] = None, cmeth: typing.Optional[typing.Callable] = None
@@ -103,6 +103,13 @@ def __init__(
103103
"""
104104
self.__instance_method = imeth
105105
self.__class_method = cmeth
106+
self.__owner = None
107+
self.__name = ""
108+
109+
def __set_name__(self, owner: typing.Any, name: str) -> None:
110+
"""Set __name__ and __objclass__ property."""
111+
self.__owner = owner
112+
self.__name = name
106113

107114
def __get__(self, instance: typing.Optional[typing.Any], owner: typing.Any) -> typing.Callable:
108115
"""Get descriptor.
@@ -129,6 +136,16 @@ def instance_method(*args: typing.Any, **kwargs: typing.Any) -> typing.Any:
129136

130137
return instance_method
131138

139+
@property
140+
def __objclass__(self) -> typing.Any: # pragma: no cover
141+
"""Read-only owner."""
142+
return self.__owner
143+
144+
@property
145+
def __name__(self) -> str: # pragma: no cover
146+
"""Read-only name."""
147+
return self.__name
148+
132149
def instance_method(self, imeth: typing.Optional[typing.Callable]) -> "SeparateClassMethod":
133150
"""Descriptor to change instance method.
134151

0 commit comments

Comments
 (0)