Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call Python non-member function referenced by class variable [duplicate]

Tags:

python

In my class I'd like to call a non-member function whose reference is stored in a member variable. My issue is that it tries to pass self to the function as the first argument. How can I avoid this?

class MyClass:
    my_useful_static_function = crcmod.mkCrcFun(0x11021, True)

    def __init__(self):
        # this gets called with the first argument as self :(
        result = self.my_useful_static_function()
like image 525
Rick Avatar asked May 08 '26 13:05

Rick


1 Answers

Use staticmethod:

class MyClass:
    my_useful_static_function = staticmethod(crcmod.mkCrcFun(0x11021, True))

    def __init__(self):
        result = self.my_useful_static_function()
like image 60
Aran-Fey Avatar answered May 10 '26 02:05

Aran-Fey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!