3

I have this class

class Test(object):
      def __init__(self, *args, **kwargs)
          pass

       def make(self):
          pass

Now i have class name and function name as variables like

class_name = "Test"
func_name = "make"

i want to call Test(**kwargs).make()

I tried this

    cls_name  = "Test"
    callback  = getattr(cls_name, "make")
    obj = callback()
user3214546
  • 6,523
  • 13
  • 51
  • 98

4 Answers4

2

Assuming your class is at global level, you can access it through globals(). (Other possible options are locals() or vars().) For example:

>>> class Test(object):
...     def __init__(self, *args, **kwargs):
...         print("in test init")
...     def make(self):
...         print("in test make")
... 
>>> cls_name = "Test"
>>> method_name = "make"
>>> callback = getattr(globals()[cls_name](), method_name)
in test init
>>> callback()
in test make

If your Test class were in a different module, you could use getattr(module_containing_test, cls_name) instead of the globals() lookup.

Patrick Maupin
  • 8,024
  • 2
  • 23
  • 42
  • what bout if make is also variable – user3214546 Oct 01 '15 at 04:25
  • @user3214546 -- updated for variable attribute access. – Patrick Maupin Oct 01 '15 at 04:28
  • what if i don't have that in globals but in cls_name ,what if i actually pass the class itsself rather the string , then what do i need to do – user3214546 Oct 01 '15 at 04:30
  • so then it will be like `getattr(getattr(module_containing_test, cls_name), method_name)()` – user3214546 Oct 01 '15 at 04:32
  • @user3214546 -- If you have the class itself, you probably want to instantiate it, and then you can get a method. If you only need one method per instantiation, then `callback = getattr(my_class(), method_name)` should work. – Patrick Maupin Oct 01 '15 at 04:32
  • 1
    @user3214546 -- close. In order to call a regular method (not a class method or static method) you need an instantiated class: `getattr(getattr(module_containing_test, cls_name)(), method_name)()` – Patrick Maupin Oct 01 '15 at 04:33
0

The below piece of code seems working and I don't know if its safe, as most developers hate eval.

class Test(object):
      def __init__(self, *args, **kwargs):
          print "I'm init"
      def make(self):
          print "I'm make"

class_name = "Test"
func_name = "make"
a = eval(class_name+"()")
eval('a'+"."+func_name+"()")
praba230890
  • 2,192
  • 21
  • 37
  • It's not particularly unsafe in this context, because you are not passing it random data, but it's not particularly efficient in this context, either. – Patrick Maupin Oct 01 '15 at 04:25
0

Some compilation of answers given here and here

# You have to prepend module name to class name

class_name = "moduletest.Test"
func_name = "make"

def get_class( kls ):
    parts = kls.split('.')
    module = ".".join(parts[:-1])
    m = __import__( module )
    for comp in parts[1:]:
        m = getattr(m, comp)
    return m

# You're creating object here, so have to pass all needed argument then
test = get_class(class_name)() 
callback = getattr(test, func_name)
obj = callback()
Community
  • 1
  • 1
0

Look for class in globals() and then invoke method via getattr.

clazz = globals()["Test"]
obj = clazz()
getattr(obj, "make")()
iimos
  • 4,767
  • 2
  • 33
  • 35