Skip to content

Commit

Permalink
pythongh-113212: Document zero-arg super() inside nested functions …
Browse files Browse the repository at this point in the history
…and generator expressions
  • Loading branch information
WolframAlph committed Dec 20, 2023
1 parent d2674cc commit adb9e79
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Doc/library/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1845,6 +1845,21 @@ are always available. They are listed here in alphabetical order.
:func:`super`, see `guide to using super()
<https://rhettinger.wordpress.com/2011/05/26/super-considered-super/>`_.

.. note::
By default, if no second argument is provided, :func:`super` uses the first argument
of the immediately enclosing function definition, which is ``self`` under normal circumstances.
This may lead to unexpected behaviour if used inside nested functions or generator expressions
(the latter creates implicit nested functions). The following example will result in an exception::

class B(C):
def method(self, arg):
list(super().method(arg) for _ in range(1)) # Raises TypeError.

In such cases, you should provide arguments explicitly::

class B(C):
def method(self, arg):
list(super(B, self).method(arg) for _ in range(1))

.. _func-tuple:
.. class:: tuple()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
improve :py:class:`super` error message && document zero-arg :py:class:`super` inside nested
functions and generator expressions

0 comments on commit adb9e79

Please sign in to comment.