0

OK, so I have a string, x = module.class.test_function(value), and I want to call it and get the response. I've tried to use getattr(module.class, test_function)(value) yet it gives the error:

AttributeError: module 'module' has no attribute 'test_function'

I'm new to these things in python, how would I do this?

marsnebulasoup
  • 2,530
  • 2
  • 16
  • 37
  • Can you show this as a running script? Is `test_function` supposed to be a variable with the function name in it? What is `thing.class`? Does `print(thing.class)` give a hint? – tdelaney May 23 '18 at 02:14
  • It looks like `thing.class` is a module so the question is, where did it come from? – tdelaney May 23 '18 at 02:14
  • Yes test_function is a variable with the function name in it – marsnebulasoup May 23 '18 at 02:15
  • The error message says that `module.class` is in fact a module called "module" and it doesn't have a variable called "test_function". There really isn't much else we can say without more information. – tdelaney May 23 '18 at 02:21
  • But what is the syntax of calling class.function(value) when class and function and value are variables cointaining the class and function names and what to input to the function. – marsnebulasoup May 23 '18 at 02:24
  • You have the right syntax. Python took the object in `module.class` and tried to find whatever was in `test_function` but it failed. `module.class` is a module called "module" and `test_function` is the string "test_function". You could `print(module.class.__dict__)` t osee what's in it and `print(module.class.__file__)` to see where it is. – tdelaney May 23 '18 at 02:37
  • But you see, it thinks that test_functuon is the function name. But it's a variable – marsnebulasoup May 23 '18 at 02:38
  • Its a variable with the string "test_function" in it. At least that's what the error message says. – tdelaney May 23 '18 at 02:39
  • Add `print('test_function:', test_function)` to see if that is the case. – tdelaney May 23 '18 at 02:40
  • I'm not 100% sure I've understood the question correctly, but I've added an example answer that shows how to do a dynamic import with `getattr` below. Does that address your question? – Taylor D. Edmiston May 23 '18 at 03:03

1 Answers1

3

Given a file my_module.py:

def my_func(greeting):
    print(f'{greeting} from my_func!')

You can import your function and call it normally like this:

>>> from my_module import my_func
>>> my_func('hello')
hello from my_func!

Alternatively, if you want to import the function dynamically with getattr:

>>> import my_module
>>> getattr(my_module, 'my_func')
<function my_func at 0x1086aa8c8>
>>> a_func = getattr(my_module, 'my_func')
>>> a_func('bonjour')
bonjour from my_func!

I would only recommend this style if it's required by your use case, for instance, the method name to be called not being known until runtime, methods being generated dynamically, or something like that.

A good answer that explains getattr in more detail is - Why use setattr() and getattr() built-ins? and you can find a bit more at http://effbot.org/zone/python-getattr.htm.

Taylor D. Edmiston
  • 12,088
  • 6
  • 56
  • 76