Python: Regular method and static method with same name

Viewed 9331

Introduction

I have a Python class, which contains a number of methods. I want one of those methods to have a static counterpart—that is, a static method with the same name—which can handle more arguments. After some searching, I have found that I can use the @staticmethod decorator to create a static method.

Problem

For convenience, I have created a reduced test case which reproduces the issue:

class myclass:

    @staticmethod
    def foo():
        return 'static method'

    def foo(self):
        return 'public method'

obj = myclass()
print(obj.foo())
print(myclass.foo())

I expect that the code above will print the following:

public method
static method

However, the code prints the following:

public method
Traceback (most recent call last):
  File "sandbox.py", line 14, in <module>
    print(myclass.foo())
TypeError: foo() missing 1 required positional argument: 'self'

From this, I can only assume that calling myclass.foo() tries to call its non-static counterpart with no arguments (which won't work because non-static methods always accept the argument self). This behavior baffles me, because I expect any call to the static method to actually call the static method.

I've tested the issue in both Python 2.7 and 3.3, only to receive the same error.

Questions

Why does this happen, and what can I do to fix my code so it prints:

public method
static method

as I would expect?

5 Answers

When you try to call MyClass.foo(), Python will complain since you did not pass the one required self argument. @coderpatros's answer has the right idea, where we provide a default value for self, so its no longer required. However, that won't work if there are additional arguments besides self. Here's a function that can handle almost all types of method signatures:

import inspect
from functools import wraps

def class_overload(cls, methods):
    """ Add classmethod overloads to one or more instance methods """
    for name in methods:
        func = getattr(cls, name)
        # required positional arguments
        pos_args = 1 # start at one, as we assume "self" is positional_only
        kwd_args = [] # [name:str, ...]
        sig = iter(inspect.signature(func).parameters.values())
        next(sig)
        for s in sig:
            if s.default is s.empty:
                if s.kind == s.POSITIONAL_ONLY:
                    pos_args += 1
                    continue
                elif s.kind == s.POSITIONAL_OR_KEYWORD:
                    kwd_args.append(s.name)
                    continue
            break
        @wraps(func)
        def overloaded(*args, **kwargs):
            # most common case: missing positional arg or 1st arg is not a cls instance
            isclass = len(args) < pos_args or not isinstance(args[0], cls)
            # handle ambiguous signatures, func(self, arg:cls, *args, **kwargs);
            # check if missing required positional_or_keyword arg
            if not isclass:
                for i in range(len(args)-pos_args,len(kwd_args)):
                    if kwd_args[i] not in kwargs:
                        isclass = True
                        break
            # class method
            if isclass:
                return func(cls, *args, **kwargs)
            # instance method
            return func(*args, **kwargs)
        setattr(cls, name, overloaded)

class Foo:
    def foo(self, *args, **kwargs):
        isclass = self is Foo
        print("foo {} method called".format(["instance","class"][isclass]))

class_overload(Foo, ["foo"])

Foo.foo() # "foo class method called"
Foo().foo() # "foo instance method called"

You can use the isclass bool to implement the different logic for class vs instance method.

The class_overload function is a bit beefy and will need to inspect the signature when the class is declared. But the actual logic in the runtime decorator (overloaded) should be quite fast.

There's one signature that this solution won't work for: a method with an optional, first, positional argument of type Foo. It's impossible to tell if we are calling the static or instance method just by the signature in this case. For example:

def bad_foo(self, other:Foo=None):
    ...
bad_foo(f) # f.bad_foo(None) or Foo.bad_foo(f) ???

Note, this solution may also report an incorrect isclass value if you pass in incorrect arguments to the method (a programmer error, so may not be important to you).

We can get a possibly more robust solution by doing the reverse of this: first start with a classmethod, and then create an instance method overload of it. This is essentially the same idea as @Dologan's answer, though I think mine is a little less boilerplatey if you need to do this on several methods:

from types import MethodType

def instance_overload(self, methods):
    """ Adds instance overloads for one or more classmethods"""
    for name in methods:
        setattr(self, name, MethodType(getattr(self, name).__func__, self))

class Foo:
    def __init__(self):
        instance_overload(self, ["foo"])

    @classmethod
    def foo(self, *args, **kwargs):
        isclass = self is Foo
        print("foo {} method called:".format(["instance","class"][isclass]))

Foo.foo() # "foo class method called"
Foo().foo() # "foo instance method called"

Not counting the code for class_overload or instance_overload, the code is equally succinct. Often signature introspection is touted as the "pythonic" way to do these kinds of things. But I think I'd recommend using the instance_method solution instead; isclass will be correct for any method signature, including cases where you call with incorrect arguments (a programmer error).

Related