8000 gh-113212: Document zero-arg `super()` inside nested functions and ge… · python/cpython@adb9e79 · GitHub
[go: up one dir, main page]

Skip to content

Commit adb9e79

Browse files
committed
gh-113212: Document zero-arg super() inside nested functions and generator expressions
1 parent d2674cc commit adb9e79

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

Doc/library/functions.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1845,6 +1845,21 @@ are always available. They are listed here in alphabetical order.
18451845
:func:`super`, see `guide to using super()
18461846
<https://rhettinger.wordpress.com/2011/05/26/super-considered-super/>`_.
18471847

1848+
.. note::
1849+
By default, if no second argument is provided, :func:`super` uses the first argument
1850+
of the immediately enclosing function definition, which is ``self`` under normal circumstances.
1851+
This may lead to unexpected behaviour if used inside nested functions or generator expressions
1852+
(the latter creates implicit nested functions). The following example will result in an exception::
1853+
1854+
class B(C):
1855+
def method(self, arg):
1856+
list(super().method(arg) for _ in range(1)) # Raises TypeError.
1857+
1858+
In such cases, you should provide arguments explicitly::
1859+
1860+
class B(C):
1861+
def method(self, arg):
1862+
list(super(B, self).method(arg) for _ in range(1))
18481863

18491864
.. _func-tuple:
18501865
.. class:: tuple()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
improve :py:class:`super` error message && document zero-arg :py:class:`super` inside nested
2+
functions and generator expressions

0 commit comments

Comments
 (0)
0