-1

I want to run a function of a module with a variable name for example:

Plugins.SomeUnknownName.function() without knowing their name but Plugins. and .function() stay the same.

I have tried several things like plugin = 'Plugins.' + unknown_plugin_name + '.function()' or

plugin_name = "SomePlugin"

Plugins.plugin_name.function()

But I don't know any further.

Maybe someone knows how to solve that.

Also this is my first stack overflow post, so please correct me if I did something wrong.

1
  • What about getattr(Plugins, plugin_name).function() (?) Commented May 12, 2021 at 19:02

1 Answer 1

0

You can use getattr to pass the string name. Using numpy as an example here's what the normal usage would look like

>>> import numpy
>>> numpy.random.randint(1, 5)
2

Now similar to your example

>>> getattr(numpy, 'random').randint(1, 5)
2

So applying to your case

getattr(Plugins, 'plugin_name').function()
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.