python - How to instantiate an object of a class from within the class itself? -
below simplified version of code:
in testclass.py file:
class testclass: def func1(self): pass def func2(self): here want call func1 and in main.py file:
testclass1 = testclass() testclass1.func2() initially, have tried run func1 func2 in following way (in testclass.py):
testclass.func2() but in case have received following error message:
typeerror: unbound method func1() must called testclass instance first argument (got str instance instead) so way understand problem there no testclass1 instance within testclass itself, exists in main(calling) code. in order fix have passed instance of testclass while calling func2 main.py:
testclass.func2(testclass1) and have added (the class) func2 mandatory argument accordingly. as result, seem working fine. wanted make sure that's it's accepted way that.
i'm not python guru seems you'll need call func1 , func2 using self reference similar this in java.
Comments
Post a Comment