File tree 2 files changed +26
-0
lines changed
Filter options
Misc/NEWS.d/next/Core and Builtins
2 files changed +26
-0
lines changed
Original file line number Diff line number Diff line change @@ -1822,6 +1822,30 @@ are always available. They are listed here in alphabetical order.
1822
1822
super().method(arg) # This does the same thing as:
1823
1823
# super(C, self).method(arg)
1824
1824
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
+
1825
1849
In addition to method lookups, :func: `super ` also works for attribute
1826
1850
lookups. One possible use case for this is calling :term: `descriptors <descriptor> `
1827
1851
in a parent or sibling class.
Original file line number Diff line number Diff line change
1
+ improve :py:class: `super ` error message && document zero-arg :py:class: `super ` inside nested
2
+ functions and generator expressions
You can’t perform that action at this time.
0 commit comments