Ask Your Question
1

Is it possible to call a get method inside a method from a different class within the main function?

asked 2022-06-02 11:00:00 +0000

david gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-02-26 06:00:00 +0000

pufferfish gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-06-02 11:00:00 +0000

Seen: 11 times

Last updated: Feb 26 '23