0

in java we can have objects of class and Method. how we can achieve same in python.

Example (in java) """Class cls=Class.forName("Test.class"); Method method = Test.class.getMethods()[];"""

2
  • There are reflection functions such as type(), dir(), isintstance(), callable(), getattr(), setattr() and others. But there is no such Java'ic thing like class of class, because everything in python is an object (even classes) Commented Apr 22, 2022 at 10:06
  • look at meta programming I think. what do you mean by function class object Commented Apr 22, 2022 at 10:07

1 Answer 1

1

You can create the class by reflection.

And with inspect built-in module, you can get methods:

import inspect


class MyClass:
    def __init__(self):
        self.a = "default"

    def method_1(self):
        pass


my_second_way_to_call_class = globals()["MyClass"] #reflection
my_third_way_to_call_class = MyClass # assignation of the object def

instance_1 = MyClass()
instance_2 = my_second_way_to_call_class()
instance_3 = my_third_way_to_call_class()

method_list = inspect.getmembers(instance_1, predicate=inspect.ismethod)
print(method_list)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.