Categories
object python reflection

Calling a function of a module by using its name (a string)

2253

How do I call a function, using a string with the function’s name? For example:

import foo
func_name = "bar"
call(foo, func_name)  # calls foo.bar()

2

  • 5

    Using eval would probably bring up some security concerns!

    – WMRamadan

    Sep 22, 2021 at 0:31

  • 9

    FYI: the language feature of accessing fields, classes and methods by dynamic names is called reflection. Might make future searches easier.

    – Nearoo

    Nov 27, 2021 at 11:30

2698

Given a module foo with method bar:

import foo
bar = getattr(foo, 'bar')
result = bar()

getattr can similarly be used on class instance bound methods, module-level methods, class methods… the list goes on.

17

  • 19

    hasattr or getattr can be used to determine if a function is defined. I had a database mapping (eventType and handling functionName) and I wanted to make sure I never “forgot” to define an event handler in my python

    – Shaun

    Jun 3, 2014 at 13:20

  • 13

    This works if you already know the module name. However, if you want the user to provide the module name as a string, this won’t work.

    – Blairg23

    Jun 21, 2014 at 7:39

  • 13

    If you need to avoid a NoneType is not callable exception, you could also employ the three-argument form of getattr: getattr(foo, ‘bar’, lambda: None). I apologize for the formatting; the stackexchange android app is apparently terrible.

    Aug 16, 2014 at 18:01


  • 6

    See also the answer provided by @sastanin if you only care for example about your local/current module’s functions.

    – NuSkooler

    Jun 19, 2015 at 22:19

  • 21

    @akki Yes, if you’re in the foo module you can use globals() to do this: methodToCall = globals()['bar']

    – Ben Hoyt

    Dec 20, 2016 at 19:06

704

  • Using locals(), which returns a dictionary with the current local symbol table:

    locals()["myfunction"]()
    
  • Using globals(), which returns a dictionary with the global symbol table:

    globals()["myfunction"]()
    

4

  • 85

    This method with globals/locals is good if the method you need to call is defined in the same module you are calling from.

    – Joelmob

    Oct 9, 2014 at 21:36

  • 1

    @Joelmob is there any other way to get an object by string out of the root namespace?

    – Nick T

    Jan 26, 2015 at 20:51


  • 1

    @NickT I am only aware of these methods, I don’t think there are any others that fill same function as these, at least I can’t think of a reason why there should be more.

    – Joelmob

    Jan 27, 2015 at 12:34

  • 2

    I’ve got a reason for you (actually what led me here): Module A has a function F that needs to call a function by name. Module B imports Module A, and invokes function F with a request to call Function G, which is defined in Module B. This call fails because, apparently, function F only runs with the globals that are defined in Module F – so globals()[‘G’] = None.

    Jan 30, 2017 at 15:18

430

Based on Patrick’s solution, to get the module dynamically as well, import it using:

module = __import__('foo')
func = getattr(module, 'bar')
func()

6

  • 102

    I do not understand that last comment. __import__ has its own right and the next sentence in the mentioned docs says: “Direct use of __import__() is rare, except in cases where you want to import a module whose name is only known at runtime”. So: +1 for the given answer.

    – hoffmaje

    May 5, 2012 at 9:33


  • 63

    Use importlib.import_module. The official docs say about __import__: “This is an advanced function that is not needed in everyday Python programming, unlike importlib.import_module().” docs.python.org/2/library/functions.html#__import__

    – glarrain

    Aug 5, 2013 at 22:07

  • 10

    @glarrain As long as you’re ok with only support 2.7 and up.

    Sep 14, 2013 at 16:54

  • 4

    @Xiong Chaimiov, importlib.import_module is supported in 3.6 . See docs.python.org/3.6/library/…

    Oct 5, 2017 at 19:28

  • 10

    @cowlinator Yes, 3.6 is part of “2.7 and up”, both in strict versioning semantics and in release dates (it came about six years later). It also didn’t exist for three years after my comment. 😉 In the 3.x branch, the module has been around since 3.1. 2.7 and 3.1 are now pretty ancient; you’ll still find servers hanging around that only support 2.6, but it’s probably worth having importlib be the standard advice nowadays.

    Oct 5, 2017 at 23:55