diff options
author | Andre Delfino <adelfino@gmail.com> | 2020-09-20 17:09:50 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-20 17:09:50 (GMT) |
commit | 778ad926cbdda0632f50acb7fac3a2a8b80dcf69 (patch) | |
tree | 3ebd4c3c2c973caf0cf62bfdf5106b6c49ba4abf /Doc | |
parent | 5c0eed7375fdd791cc5e19ceabfab4170ad44062 (diff) | |
download | cpython-778ad926cbdda0632f50acb7fac3a2a8b80dcf69.zip cpython-778ad926cbdda0632f50acb7fac3a2a8b80dcf69.tar.gz cpython-778ad926cbdda0632f50acb7fac3a2a8b80dcf69.tar.bz2 |
[doc] Teach 0-args form of super in Programming FAQ (GH-22176)
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/faq/programming.rst | 15 |
1 files changed, 7 insertions, 8 deletions
diff --git a/Doc/faq/programming.rst b/Doc/faq/programming.rst index eecbbf4..fd0adc3 100644 --- a/Doc/faq/programming.rst +++ b/Doc/faq/programming.rst @@ -1504,20 +1504,19 @@ Most :meth:`__setattr__` implementations must modify ``self.__dict__`` to store local state for self without causing an infinite recursion. -How do I call a method defined in a base class from a derived class that overrides it? --------------------------------------------------------------------------------------- +How do I call a method defined in a base class from a derived class that extends it? +------------------------------------------------------------------------------------ Use the built-in :func:`super` function:: class Derived(Base): def meth(self): - super(Derived, self).meth() + super().meth() # calls Base.meth -For version prior to 3.0, you may be using classic classes: For a class -definition such as ``class Derived(Base): ...`` you can call method ``meth()`` -defined in ``Base`` (or one of ``Base``'s base classes) as ``Base.meth(self, -arguments...)``. Here, ``Base.meth`` is an unbound method, so you need to -provide the ``self`` argument. +In the example, :func:`super` will automatically determine the instance from +which it was called (the ``self`` value), look up the :term:`method resolution +order` (MRO) with ``type(self).__mro__``, and return the next in line after +``Derived`` in the MRO: ``Base``. How can I organize my code to make it easier to change the base class? |