Why do I sometimes need to include "self" as an argument when defining functions in external Python modules?

Viewed 54

Say I have two Python functions defined in a module called funcs.py:

def my_fun1(X):
    return X * 2


def my_fun2(X):
    return X / 2

and a dataclass defined in a file called testclass.py:

from dataclasses import dataclass
import funcs


@dataclass
class TestClass:
    MY_FUN = funcs.my_fun1
    MY_FUNS = [funcs.my_fun1, funcs.my_fun2]
    N_X = [3, 4]

If I then run some test code:

import numpy as np
from testclass import TestClass

tc = TestClass()

data = [np.random.randn(x, 2) for x in tc.N_X]

print(tc.MY_FUN(data[0]))

it gives me the error:

TypeError: my_fun1() takes 1 positional argument but 2 were given

If I then change the definition of my_fun1 to:

def my_fun1(self, X):

It works fine, but when I try:

print(tc.MY_FUNS[0](data[0]))

I get the error:

TypeError: my_fun1() missing 1 required positional argument: 'X'

I can get around this by removing the self from the definitions and declaring a single function in the test class as a singleton list, e.g., MY_FUN = [funcs.my_fun1], however this feels like a bit of a "hacky" work-around, and I would like to be able to declare either a single function or list of functions without having to change the function definitions down the track.

Is there any way of doing this? Why does Python need the self argument sometimes, and not others in this instance?

0 Answers
Related