|
INTERVIEW QUESTIONS
WEB
PYTHON
DETAILS
Question: How do I use strings to call functions/methods?
Answer: There are various techniques.
* The best is to use a dictionary that maps strings to functions. The primary advantage of this technique is that the strings do not need to match the names of the functions. This is also the primary technique used to emulate a case construct:
def a(): pass
def b(): pass
dispatch = {'go': a, 'stop': b} # Note lack of parens for funcs
dispatch[get_input()]() # Note trailing parens to call function * Use the built-in function getattr():
import foo getattr(foo, 'bar')()
Note that getattr() works on any object, including classes, class instances, modules, and so on.
This is used in several places in the standard library, like this:
class Foo: def do_foo(self): ... def do_bar(self): ...
f = getattr(foo_instance, 'do_' + opname) f()
* Use locals() or eval() to resolve the function name:
def myFunc(): print "hello"
fname = "myFunc"
f = locals()[fname] f()
f = eval(fname) f()
Note: Using eval() is slow and dangerous. If you don't have absolute control over the contents of the string, someone could pass a string that resulted in an arbitrary function being executed.
|
|
|
Category |
Python Interview Questions & Answers -
Exam Mode /
Learning Mode
|
Rating |
(0.2) By 9044 users |
Added on |
9/23/2014 |
Views |
71468 |
Rate it! |
|
|
Question:
How do I use strings to call functions/methods?
Answer:
There are various techniques.
* The best is to use a dictionary that maps strings to functions. The primary advantage of this technique is that the strings do not need to match the names of the functions. This is also the primary technique used to emulate a case construct:
def a(): pass
def b(): pass
dispatch = {'go': a, 'stop': b} # Note lack of parens for funcs
dispatch[get_input()]() # Note trailing parens to call function * Use the built-in function getattr():
import foo getattr(foo, 'bar')()
Note that getattr() works on any object, including classes, class instances, modules, and so on.
This is used in several places in the standard library, like this:
class Foo: def do_foo(self): ... def do_bar(self): ...
f = getattr(foo_instance, 'do_' + opname) f()
* Use locals() or eval() to resolve the function name:
def myFunc(): print "hello"
fname = "myFunc"
f = locals()[fname] f()
f = eval(fname) f()
Note: Using eval() is slow and dangerous. If you don't have absolute control over the contents of the string, someone could pass a string that resulted in an arbitrary function being executed. Source: CoolInterview.com
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|
Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.
View All Python Interview Questions & Answers - Exam Mode /
Learning Mode
|