Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Yes, it is possible to call a get method from a different class within the main function by creating an object of the class containing the get method and then calling the method using the object. Here's an example:

class MyClass:
    def __init__(self, value):
        self.value = value

    def get_value(self):
        return self.value

class AnotherClass:
    def __init__(self, obj):
        self.obj = obj

    def print_value(self):
        print(self.obj.get_value())

my_object = MyClass(10)
another_object = AnotherClass(my_object)
another_object.print_value()  # This will print 10

In the above example, we have two classes - MyClass and AnotherClass. The MyClass has a get method called get_value which returns the value attribute of the class. The AnotherClass takes an object of MyClass as an input and saves it as an attribute. It also has a method called print_value which calls the get_value method of the input MyClass object and prints the returned value.

In the main function, we first create an object of MyClass called my_object with a value attribute of 10. Then, we create an object of AnotherClass called another_object with the my_object object as input. Finally, we call the print_value method of another_object which will call the get_value method of my_object and print the returned value of 10.