I have this script main.py
:
class Test:
def __init__(self):
self.arg = 'argg!!!'
def meth_1(self , arg):
print(arg + self.arg)
a = Test()
a.meth_1('po')
#### poargg!!!
def meth_2(self, arg):
print(3*arg + self.arg)
Test.meth_1 = meth_2
b = Test()
b.meth_1('lo')
#### lololoargg!!!
class Test2:
def __init__(self):
self.arg = 'argg!!!'
self.meth_1 = meth_2
c = Test2()
c.meth_1('ho') #TypeError: meth_2() missing 1 required positional argument: 'arg'if def meth_2(self, arg):
# TypeError: meth_2() takes 1 positional argument but 2 were given if def meth_2(arg):
Is there any way to add a function to a class as method at __init__
making it access class attributes ? If not why ? That is in my case have c.meth_1('ho')
working ? Sorry for the poor phrasing of my question I am a Python newbie