mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
bpo-43682: Make staticmethod objects callable (GH-25117)
Static methods (@staticmethod) are now callable as regular functions.
This commit is contained in:
parent
53114ffef1
commit
553ee2781a
7 changed files with 32 additions and 14 deletions
|
@ -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::
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue