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()

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

6 Comments

what bout if make is also variable
@user3214546 -- updated for variable attribute access.
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
so then it will be like getattr(getattr(module_containing_test, cls_name), method_name)()
@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)()
|
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+"()")

1 Comment

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.
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()

Comments

0

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

clazz = globals()["Test"]
obj = clazz()
getattr(obj, "make")()

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.