How to, if possible, “unbound” bounded method?

I have a function that I can’t modify as it’s a 3rd party, lets say it’s an add function.

def add(a, b):
    print(a + b)

For this example lets call it in init. It has to be assigned to the class as a class variable. That way it will automatically become a bounded method. In order to prevent “bounding” I need to decorate it with a staticmethod.

class Base:
    func: Callable

    def __init__(self):
        self.func(a=1, b=2)

class Working(Base):
    func = staticmethod(add)

And it is working fine as that way self is not passed to the function.

I was wondering if it’s possible to “unbound” / make it static in init

class NotWorking(Base):
    func = add

    def __init__(self):
        staticmethod(self.func)(a=1, b=2)

It has to be reusable from the Base class so I don’t need to rewrite the same thing over and over.

class NotWorking2(Base):
    func = subtract

It’s not something I need, I was just wondering if it’s possible to “unbound” it once it was bounded.

In Working class it’s just a function

<function add at 0x7f1be89de1f0>

but in NotWorking it’s either

<bound method add of <__main__.NotWorking object at 0x7f0ed11e47f0>>

or a staticmethod which results in

TypeError: 'staticmethod' object is not callable

I have seen ‘staticmethod’ object is not callable but it doesn’t really answer my question.

I don’t think it’s possible but is there a cleaner way so the staticmethod can be called only once if I have plenty of such classes?


The simplest way is to simply not call it as a method on self:

class Base:
    func: typing.ClassVar[Callable]

    def __init__(self):
        type(self).func(a=1, b=2)

class NowWorking(Base):
    func = add

Original answer:

class NowWorking(Base):
    func = add

    def __init__(self):
        NowWorking.func(a=1, b=2)

Leave a Reply

Your email address will not be published. Required fields are marked *