'How to call method in the class?

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.



Solution 1:[1]

class TestClass:
    def repeat(self, txt:str, num:int):
        counter = 0
        while counter < num:
            print(txt)
            counter = counter + 1

Add an argument (self)to the def definition. You can also initialize your class like this:

testing2 = TestClass()
testing2.repeat('test', 10)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andromeda