bpo-43682: Make staticmethod objects callable (GH-25117)

Static methods (@staticmethod) are now callable as regular functions.
This commit is contained in:
Victor Stinner 2021-04-12 00:21:22 +02:00 committed by GitHub
parent 53114ffef1
commit 553ee2781a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 32 additions and 14 deletions

View file

@ -1619,8 +1619,9 @@ are always available. They are listed here in alphabetical order.
The ``@staticmethod`` form is a function :term:`decorator` -- see
:ref:`function` for details.
A static method can be called either on the class (such as ``C.f()``) or on an instance (such
as ``C().f()``).
A static method can be called either on the class (such as ``C.f()``) or on
an instance (such as ``C().f()``). Moreover, they can be called as regular
functions (such as ``f()``).
Static methods in Python are similar to those found in Java or C++. Also see
:func:`classmethod` for a variant that is useful for creating alternate class
@ -1632,15 +1633,19 @@ are always available. They are listed here in alphabetical order.
body and you want to avoid the automatic transformation to instance
method. For these cases, use this idiom::
def regular_function():
...
class C:
builtin_open = staticmethod(open)
method = staticmethod(regular_function)
For more information on static methods, see :ref:`types`.
.. versionchanged:: 3.10
Static methods now inherit the method attributes (``__module__``,
``__name__``, ``__qualname__``, ``__doc__`` and ``__annotations__``) and
have a new ``__wrapped__`` attribute.
``__name__``, ``__qualname__``, ``__doc__`` and ``__annotations__``),
have a new ``__wrapped__`` attribute, and are now callable as regular
functions.
.. index::