Skip to content

Commit 778ad92

Browse files
[doc] Teach 0-args form of super in Programming FAQ (GH-22176)
1 parent 5c0eed7 commit 778ad92

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

Doc/faq/programming.rst

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,20 +1504,19 @@ Most :meth:`__setattr__` implementations must modify ``self.__dict__`` to store
15041504
local state for self without causing an infinite recursion.
15051505

15061506

1507-
How do I call a method defined in a base class from a derived class that overrides it?
1508-
--------------------------------------------------------------------------------------
1507+
How do I call a method defined in a base class from a derived class that extends it?
1508+
------------------------------------------------------------------------------------
15091509

15101510
Use the built-in :func:`super` function::
15111511

15121512
class Derived(Base):
15131513
def meth(self):
1514-
super(Derived, self).meth()
1514+
super().meth() # calls Base.meth
15151515

1516-
For version prior to 3.0, you may be using classic classes: For a class
1517-
definition such as ``class Derived(Base): ...`` you can call method ``meth()``
1518-
defined in ``Base`` (or one of ``Base``'s base classes) as ``Base.meth(self,
1519-
arguments...)``. Here, ``Base.meth`` is an unbound method, so you need to
1520-
provide the ``self`` argument.
1516+
In the example, :func:`super` will automatically determine the instance from
1517+
which it was called (the ``self`` value), look up the :term:`method resolution
1518+
order` (MRO) with ``type(self).__mro__``, and return the next in line after
1519+
``Derived`` in the MRO: ``Base``.
15211520

15221521

15231522
How can I organize my code to make it easier to change the base class?

0 commit comments

Comments
 (0)