|
INTERVIEW QUESTIONS
WEB
PYTHON
DETAILS
Question: How do you make a higher order function in Python?
Answer: You have two choices: you can use nested scopes or you can use callable objects. For example, suppose you wanted to define linear(a,b) which returns a function f(x) that computes the value a*x+b. Using nested scopes:
def linear(a,b): def result(x): return a*x + b return result
Or using a callable object:
class linear: def __init__(self, a, b): self.a, self.b = a,b def __call__(self, x): return self.a * x + self.b
In both cases:
taxes = linear(0.3,2)
gives a callable object where taxes(10e6) == 0.3 * 10e6 + 2.
|
|
|
Category |
Python Interview Questions & Answers -
Exam Mode /
Learning Mode
|
Rating |
(0.3) By 8863 users |
Added on |
4/28/2014 |
Views |
71268 |
Rate it! |
|
|
Question:
How do you make a higher order function in Python?
Answer:
You have two choices: you can use nested scopes or you can use callable objects. For example, suppose you wanted to define linear(a,b) which returns a function f(x) that computes the value a*x+b. Using nested scopes:
def linear(a,b): def result(x): return a*x + b return result
Or using a callable object:
class linear: def __init__(self, a, b): self.a, self.b = a,b def __call__(self, x): return self.a * x + self.b
In both cases:
taxes = linear(0.3,2)
gives a callable object where taxes(10e6) == 0.3 * 10e6 + 2. 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.
|
|
Related Questions |
View Answer |
|
How can I pass optional or keyword parameters from one function to another?
|
View Answer
|
|
How do I share global variables across modules?
|
View Answer
|
|
What are the rules for local and global variables in Python?
|
View Answer
|
|
How do you set a global variable in a function?
|
View Answer
|
|
Is there a tool to help find bugs or perform static analysis?
|
View Answer
|
|
Why can't I use an assignment in an expression?
|
View Answer
|
|
What is Python?
|
View Answer
|
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
|