I'm using classes to practice but I don't know how to use them properly.
Here I want to put the class in another file, the code inside is:
class TestClass:
def repeat(txt:str, num:int):
counter = 0
while counter < num:
print(txt)
counter = counter + 1
I can't call the method after I created the object. Here is the code:
testing2 = testing.TestClass()
testing2.repeat('test', 10)
Error:
#the error is: TypeError: TestClass.repeat() takes 2 positional arguments but 3 were given
I think this is a small problem, but explaining its solution will help my understanding a great deal.
When you creating a class, you need to follow the basic principles of class realisation. You forgot about __init__
function which initialize you class, you code need to looks like:
class TestClass:
def __init__(self):
super().__init__()
def repeat(self, txt:str, num:int):
counter = 0
while counter < num:
print(txt)
counter = counter + 1
And then all will works
testing2 = TestClass()
testing2.repeat('test', 10)
#test
#test
#test
#test
#test
#test
#test
#test
#test
#test