call a method on an instance of a class in python

To call a method on an instance of a class in Python, you first need to create an instance of the class. This can be done by calling the class name, followed by any arguments that are required by the class's __init__ method. Once you have an instance, you can call any public method defined by the class on that instance by using the dot notation:

main.py
class MyClass:
    def my_method(self):
        print("Hello, World!")
        
my_instance = MyClass()
my_instance.my_method() # prints "Hello, World!"
153 chars
7 lines

In the above example, we define a class MyClass with a single method my_method, which simply prints "Hello, World!". We then create an instance of MyClass called my_instance, and call the my_method method on that instance using the dot notation. This causes the string "Hello, World!" to be printed to the console.

gistlibby LogSnag