This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vstinner
Recipients vstinner
Date 2021-03-31.14:31:21
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
Currently, static methods created by the @staticmethod decorator are not callable as regular function. Example:
def func():
    print("my func")

class MyClass:
    method = func

func() # A: regular function
MyClass.method() # B: class method
MyClass().method() # C: instance method

The func() call raises TypeError('staticmethod' object is not callable) exception.

I propose to make staticmethod objects callable to get a similar to built-in function:
func = len

class MyClass:
    method = func

func("abc") # A: regular function
MyClass.method("abc") # B: class method
MyClass().method("abc") # C: instance method

The 3 variants (A, B, C) to call the built-in len() function work just as expected.

If static method objects become callable, the 3 variants (A, B, C) will just work.

It would avoid the hack like _pyio.Wrapper:
class DocDescriptor:
    """Helper for
    def __get__(self, obj, typ=None):
        return (
            "open(file, mode='r', buffering=-1, encoding=None, "
                 "errors=None, newline=None, closefd=True)\n\n" +

class OpenWrapper:
    """Wrapper for

    Trick so that open won't become a bound method when stored
    as a class variable (as dbm.dumb does).

    See initstdio() in Python/pylifecycle.c.
    __doc__ = DocDescriptor()

    def __new__(cls, *args, **kwargs):
        return open(*args, **kwargs)

Currently, it's not possible possible to use directly as a method:
class MyClass:
    method =

whereas "method =" just works because is a built-in function.

See also bpo-43680 "Remove undocumented io.OpenWrapper and _pyio.OpenWrapper" and my thread on python-dev:

"Weird io.OpenWrapper hack to use a function as method"
Date User Action Args
2021-03-31 14:31:21vstinnersetrecipients: + vstinner
2021-03-31 14:31:21vstinnersetmessageid: <>
2021-03-31 14:31:21vstinnerlinkissue43682 messages
2021-03-31 14:31:21vstinnercreate