bpo-43682: Make staticmethod objects callable (GH-25117)
Static methods (@staticmethod) are now callable as regular functions.
diff --git a/Doc/library/functions.rst b/Doc/library/functions.rst
index dca8b93..30f62e6 100644
--- a/Doc/library/functions.rst
+++ b/Doc/library/functions.rst
@@ -1619,8 +1619,9 @@
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 @@
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::
diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst
index 9c819b7..3b8780d 100644
--- a/Doc/reference/datamodel.rst
+++ b/Doc/reference/datamodel.rst
@@ -1132,9 +1132,8 @@
around any other object, usually a user-defined method object. When a static
method object is retrieved from a class or a class instance, the object actually
returned is the wrapped object, which is not subject to any further
- transformation. Static method objects are not themselves callable, although the
- objects they wrap usually are. Static method objects are created by the built-in
- :func:`staticmethod` constructor.
+ transformation. Static method objects are also callable. Static method
+ objects are created by the built-in :func:`staticmethod` constructor.
Class method objects
A class method object, like a static method object, is a wrapper around another
diff --git a/Doc/whatsnew/3.10.rst b/Doc/whatsnew/3.10.rst
index 50c8d53..9f6b7a4 100644
--- a/Doc/whatsnew/3.10.rst
+++ b/Doc/whatsnew/3.10.rst
@@ -623,6 +623,7 @@
(:func:`@classmethod <classmethod>`) now inherit the method attributes
(``__module__``, ``__name__``, ``__qualname__``, ``__doc__``,
``__annotations__``) and have a new ``__wrapped__`` attribute.
+ Moreover, static methods are now callable as regular functions.
(Contributed by Victor Stinner in :issue:`43682`.)