In python 3, is there a way to assign a function to a method of an instance (if I may say so)? I'm not talking about class method overriding
for example :
class MyClass:
def __init__(self, a):
self.a=a
self.b=0
def set_b(self):
pass
def fun(x):
print(x)
x.b=2*x.a
my=MyClass(2)
my.set_b=fun
my.set_b
print(my.b)
The output is 0. I would like it to be 4. I'm not very experienced and I think I don't have the right approach. I wanted to set set_b dynamically.
Edit
I corrected the line :
my.set_b
to :
my.set_b()
and I get the error:
Traceback (most recent call last): File "test.py", line 20, in my.set_b() TypeError: fun() missing 1 required positional argument: 'x'