I am having some problems with the self parameter, and some seemingly inconsistent behavior in Python annoy me, so I find it better to ask some people in the know. I have a class, Foo . This class will have a bunch of m1 methods, via mN . For some of them, I will use the standard definition, as in the case of m1 below. But for others, itโs more convenient to simply assign the method name directly, as I did with m2 and m3 .
import os def myfun(x, y): return x + y class Foo(): def m1(self, y, z): return y + z + 42 m2 = os.access m3 = myfun f = Foo() print f.m1(1, 2) print f.m2("/", os.R_OK) print f.m3(3, 4)
Now I know that os.access does not accept the self parameter (apparently). And it still has no problems with this type of destination. However, I cannot do the same for my own modules (imagine myfun in mymodule.myfun ). Executing the above code gives the following output:
3 True Traceback (most recent call last): File "foo.py", line 16, in <module> print f.m3(3, 4) TypeError: myfun() takes exactly 2 arguments (3 given)
The problem is that, because of the framework in which I work, I cannot escape at least the Foo class. But I would like to avoid using my mymodule material in a dummy class. To do this, I need to do something ala
def m3(self,a1, a2): return mymodule.myfun(a1,a2)
This is very redundant when you have 20 of them. So the question is how can I do this in a completely different and obviously much smarter way, or how can I make my own modules behave as built-in ones, so he doesn't complain that he gets too much of one argument.