Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4973555

Browse filesBrowse files
committed
gh-113212: Document zero-arg super() inside nested functions and generator expressions
1 parent 09d3ddb commit 4973555
Copy full SHA for 4973555

File tree

2 files changed

+26
-0
lines changed
Filter options

2 files changed

+26
-0
lines changed

‎Doc/library/functions.rst

Copy file name to clipboardExpand all lines: Doc/library/functions.rst
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1822,6 +1822,30 @@ are always available. They are listed here in alphabetical order.
18221822
super().method(arg) # This does the same thing as:
18231823
# super(C, self).method(arg)
18241824

1825+
.. warning::
1826+
By default, if no second argument is provided, :func:`super` uses first argument
1827+
of immediately enclosing function definition which is `self` under normal circumstances.
1828+
This may lead to unexpected behaviour if used inside nested functions or generator expressions
1829+
as the latter creates implicit nested function. Following example will result in exception::
1830+
1831+
class A:
1832+
def method(self):
1833+
return 1
1834+
1835+
class B(A):
1836+
def method(self):
1837+
return list(super().method() for _ in range(3)) # Expected [1, 1, 1] but TypeError will be raised.
1838+
1839+
In such cases, you should provide arguments explicitly::
1840+
1841+
class A:
1842+
def method(self):
1843+
return 1
1844+
1845+
class B(A):
1846+
def method(self):
1847+
return list(super(B, self).method() for _ in range(3)) # Will return [1, 1, 1].
1848+
18251849
In addition to method lookups, :func:`super` also works for attribute
18261850
lookups. One possible use case for this is calling :term:`descriptors <descriptor>`
18271851
in a parent or sibling class.
+2Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.