Skip to content

Commit e722141

Browse files
committed
improve code example in the docstring
1 parent eecfe28 commit e722141

File tree

4 files changed

+92
-62
lines changed

4 files changed

+92
-62
lines changed

Doc/library/types.rst

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -523,9 +523,39 @@ Additional Utility Classes and Functions
523523

524524
.. function:: lookup_special_method(obj, attr, /)
525525

526-
Do a method lookup in the type without looking in the instance dictionary
527-
but still binding it to the instance. Returns ``None`` if the method is not
528-
found.
526+
Lookup special method name ``attr`` on ``obj``.
527+
528+
Lookup method ``attr`` on ``obj`` without looking in the instance
529+
dictionary. For methods defined in class ``__dict__`` or ``__slots__``, it
530+
returns the unbound function (descriptor), not a bound method. The
531+
caller is responsible for passing the object as the first argument when
532+
calling it:
533+
534+
.. code-block:: python
535+
536+
>>> class A:
537+
... def __enter__(self):
538+
... return "A.__enter__"
539+
...
540+
>>> class B:
541+
... __slots__ = ("__enter__",)
542+
... def __init__(self):
543+
... def __enter__(self):
544+
... return "B.__enter__"
545+
... self.__enter__ = __enter__
546+
...
547+
>>> a = A()
548+
>>> b = B()
549+
>>> enter_a = types.lookup_special_method(a, "__enter__")
550+
>>> enter_b = types.lookup_special_method(b, "__enter__")
551+
>>> enter_a(a)
552+
'A.__enter__'
553+
>>> enter_b(b)
554+
'B.__enter__'
555+
556+
For other descriptors (property, etc.), it returns the result of the
557+
descriptor's ``__get__`` method. Returns ``None`` if the method is not
558+
found.
529559

530560
.. versionadded:: next
531561

Lib/types.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -90,25 +90,25 @@ def lookup_special_method(obj, attr, /):
9090
caller is responsible for passing the object as the first argument when
9191
calling it:
9292
93-
class A:
94-
def __enter__(self):
95-
pass
96-
97-
class B:
98-
__slots__ = ("__enter__",)
99-
100-
def __init__(self):
101-
def __enter__(self):
102-
pass
103-
self.__enter__ = __enter__
104-
105-
a = A()
106-
b = B()
107-
enter_a = types.lookup_special_method(a, "__enter__")
108-
enter_b = types.lookup_special_method(b, "__enter__")
109-
110-
result_a = enter_a(a)
111-
result_b = enter_b(b)
93+
>>> class A:
94+
... def __enter__(self):
95+
... return "A.__enter__"
96+
...
97+
>>> class B:
98+
... __slots__ = ("__enter__",)
99+
... def __init__(self):
100+
... def __enter__(self):
101+
... return "B.__enter__"
102+
... self.__enter__ = __enter__
103+
...
104+
>>> a = A()
105+
>>> b = B()
106+
>>> enter_a = types.lookup_special_method(a, "__enter__")
107+
>>> enter_b = types.lookup_special_method(b, "__enter__")
108+
>>> enter_a(a)
109+
'A.__enter__'
110+
>>> enter_b(b)
111+
'B.__enter__'
112112
113113
For other descriptors (property, etc.), it returns the result of the
114114
descriptor's `__get__` method. Returns `None` if the method is not

Modules/_typesmodule.c

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,25 @@ returns the unbound function (descriptor), not a bound method. The
3030
caller is responsible for passing the object as the first argument when
3131
calling it:
3232
33-
class A:
34-
def __enter__(self):
35-
pass
36-
37-
class B:
38-
__slots__ = ("__enter__",)
39-
40-
def __init__(self):
41-
def __enter__(self):
42-
pass
43-
self.__enter__ = __enter__
44-
45-
a = A()
46-
b = B()
47-
enter_a = types.lookup_special_method(a, "__enter__")
48-
enter_b = types.lookup_special_method(b, "__enter__")
49-
50-
result_a = enter_a(a)
51-
result_b = enter_b(b)
33+
>>> class A:
34+
... def __enter__(self):
35+
... return "A.__enter__"
36+
...
37+
>>> class B:
38+
... __slots__ = ("__enter__",)
39+
... def __init__(self):
40+
... def __enter__(self):
41+
... return "B.__enter__"
42+
... self.__enter__ = __enter__
43+
...
44+
>>> a = A()
45+
>>> b = B()
46+
>>> enter_a = types.lookup_special_method(a, "__enter__")
47+
>>> enter_b = types.lookup_special_method(b, "__enter__")
48+
>>> enter_a(a)
49+
'A.__enter__'
50+
>>> enter_b(b)
51+
'B.__enter__'
5252
5353
For other descriptors (property, etc.), it returns the result of the
5454
descriptor's `__get__` method. Returns `None` if the method is not
@@ -58,7 +58,7 @@ found.
5858
static PyObject *
5959
_types_lookup_special_method_impl(PyObject *module, PyObject *obj,
6060
PyObject *attr)
61-
/*[clinic end generated code: output=890e22cc0b8e0d34 input=fca9cb0e313a7848]*/
61+
/*[clinic end generated code: output=890e22cc0b8e0d34 input=e317288370125cd5]*/
6262
{
6363
if (!PyUnicode_Check(attr)) {
6464
PyErr_Format(PyExc_TypeError,

Modules/clinic/_typesmodule.c.h

Lines changed: 20 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)