Assuming module foo
with method bar
:
import foo
method_to_call = getattr(foo, 'bar')
result = method_to_call()
You could shorten lines 2 and 3 to:
result = getattr(foo, 'bar')()
if that makes more sense for your use case.
You can use getattr
in this fashion on class instance bound methods, module-level methods, class methods… the list goes on.
Related Contents:
- How to access object attribute given string corresponding to name of that attribute
- How to print instances of a class using print()?
- How do I determine the size of an object in Python?
- How to build a basic iterator?
- Why do Python classes inherit object?
- How do you programmatically set an attribute?
- Spawning multiple instances of the same object concurrently in python
- How do I call a parent class’s method from a child class in Python?
- Convert nested Python dict to object?
- Saving and loading multiple objects in pickle file?
- Is everything an object in Python like Ruby?
- How can I create a copy of an object in Python?
- id()s of bound and unbound method objects — sometimes the same for different objects, sometimes different for the same object
- Referring to the null object in Python
- Getting an instance name inside class __init__() [duplicate]
- What is the difference between __init__ and __call__?
- Accessing Object Memory Address
- Elegant way to check if a nested key exists in a dict?
- Why is `self` in Python objects immutable?
- Using self.xxxx as a default parameter – Python
- How do I save and restore multiple variables in python?
- How to convert a nested Python dict to object?
- super() fails with error: TypeError “argument 1 must be type, not classobj” when parent does not inherit from object
- Python dictionary from an object’s fields
- How can I separate the functions of a class into multiple files?
- Null object in Python
- How can I create an object and add attributes to it?
- Why doesn’t assigning to the loop variable modify the original list? How can I assign back to the list in a loop? [duplicate]
- Difference between ‘cls’ and ‘self’ in Python classes?
- Why do I get a TypeError that says “takes no arguments (1 given)”? [duplicate]
- Why is `object` an instance of `type` and `type` an instance of `object`?
- How to check if an object has an attribute?
- How to access (get or set) object attribute given string corresponding to name of that attribute
- What is the purpose of subclassing the class “object” in Python?
- How to clone a Python generator object?
- How do I correctly setup and teardown for my pytest class with tests?
- Why do ints require three times as much memory in Python?
- How to apply __str__ function when printing a list of objects in Python [duplicate]
- Saving and loading objects and using pickle
- Evaluation of boolean expressions in Python
- Dictionary vs Object – which is more efficient and why?
- Getting container/parent object from within python
- What does object’s __init__() method do in python? [duplicate]
- Why does object.__new__ work differently in these three cases
- How to use “get_or_create()” in Django?
- What happens when you assign the value of one variable to another variable in Python?
- Creating nested dataclass objects in Python
- How do I check if an object has an attribute?
- Play a Sound with Python [duplicate]
- formatting long numbers as strings in python