Quantcast
Channel: Calling a function of a module by using its name (a string) - Stack Overflow
Browsing all 20 articles
Browse latest View live

Answer by Number File for Calling a function of a module by using its name (a...

This is a simple answer, this will allow you to clear the screen for example. There are two examples below, with eval and exec, that will print 0 at the top after cleaning (if you're using Windows,...

View Article



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

As this question How to dynamically call methods within a class using method-name assignment to a variable [duplicate] marked as a duplicate as this one, I am posting a related answer here: The...

View Article

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

Try this. While this still uses eval, it only uses it to summon the function from the current context. Then, you have the real function to use as you wish. The main benefit for me from this is that you...

View Article

Answer by user3946687 for Calling a function of a module by using its name (a...

The best answer according to the Python programming FAQ would be: functions = {'myfoo': foo.bar} mystring = 'myfoo' if mystring in functions: functions[mystring]() The primary advantage of this...

View Article

Answer by 00500005 for Calling a function of a module by using its name (a...

The answer (I hope) no one ever wanted Eval like behavior getattr(locals().get("foo") or globals().get("foo"), "bar")() Why not add auto-importing getattr( locals().get("foo") or globals().get("foo")...

View Article


Answer by ferrouswheel for Calling a function of a module by using its name...

Given a string, with a complete python path to a function, this is how I went about getting the result of said function: import importlib function_string = 'mypackage.mymodule.myfunc' mod_name,...

View Article

Answer by Natdrip for Calling a function of a module by using its name (a...

none of what was suggested helped me. I did discover this though. <object>.__getattribute__(<string name>)(<params>) I am using python 2.66 Hope this helps

View Article

Answer by Sourcegeek for Calling a function of a module by using its name (a...

Just a simple contribution. If the class that we need to instance is in the same file, we can use something like this: # Get class from globals and create an instance m = globals()['our_class']() # Get...

View Article


Answer by trubliphone for Calling a function of a module by using its name (a...

For what it's worth, if you needed to pass the function (or class) name and app name as a string, then you could do this: myFnName = "MyFn" myAppName = "MyApp" app = sys.modules[myAppName] fn =...

View Article


Answer by sastanin for Calling a function of a module by using its name (a...

locals()["myfunction"]() or globals()["myfunction"]() locals returns a dictionary with a current local symbol table. globals returns a dictionary with global symbol table.

View Article

Answer by HS. for Calling a function of a module by using its name (a string)

Patrick's solution is probably the cleanest. If you need to dynamically pick up the module as well, you can import it like: module = __import__('foo') func = getattr(module, 'bar') func()

View Article

Answer by Patrick Johnmeyer for Calling a function of a module by using its...

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

View Article

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

What is the best way to go about calling a function given a string with the function's name in a Python program. For example, let's say that I have a module foo, and I have a string whose content is...

View Article


Answer by 정도유 for Calling a function of a module by using its name (a string)

getattr calls method by name from an object.But this object should be parent of calling class.The parent class can be got by super(self.__class__, self)class Base: def call_base(func):"""This does not...

View Article

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

Although getattr() is elegant (and about 7x faster) method, you can get return value from the function (local, class method, module) with eval as elegant as x = eval('foo.bar')(). And when you...

View Article


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

In many, many use cases, the answer is 'Don't!'Instead do something like:safe_functions = {'baz':foo.baz, 'bar':foo.bar}safe_functions['bar']()safe_functions['delete_all_the_things']()

View Article

Answer by U12-F̉͋̅̾̇orward for Calling a function of a module by using its...

Nobody mentioned operator.attrgetter yet:>>> from operator import attrgetter>>> l = [1, 2, 3]>>> attrgetter('reverse')(l)()>>> l[3, 2, 1]>>>

View Article


Answer by Aliakbar Ahmadi for Calling a function of a module by using its...

In python3, you can use the __getattribute__ method. See following example with a list method name string:func_name = 'reverse'l = [1, 2, 3, 4]print(l)>> [1, 2, 3,...

View Article

Answer by Bowen 404 for Calling a function of a module by using its name (a...

i'm facing the similar problem before, which is to convert a string to a function. but i can't use eval() or ast.literal_eval(), because i don't want to execute this code immediately.e.g. i have a...

View Article

Answer by Franz Kurt for Calling a function of a module by using its name (a...

You means get the pointer to an inner function from a moduleimport foomethod = foo.barexecuted = method(parameter)This is not a better pythonic way indeed is possible for punctual cases

View Article
Browsing all 20 articles
Browse latest View live




Latest Images