How do you make a higher order function in Python?

0 votes
How to make a higher-order function in Python? Can it be done by using callable objects?
Jul 25 in Python by Isha

1 answer to this question.

0 votes

You have two choices to create higher-order functions: you can use nested scopes or you can use callable objects. For example, suppose you wanted to define line(a,b) which returns a function f(x) that computes the value a*x+b. 

Using nested scopes:

def line(a, b):
    def result(x):
        return a * x + b
    return result

Or using a callable object:

class line:

    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 = line(0.3, 2)
answered Jul 25 by Yesha

Related Questions In Python

+2 votes
1 answer

How do you make a block comment in python?

''' This is a multiline comment. I ...READ MORE

answered Aug 23, 2018 in Python by Priyaj
• 56,920 points
0 votes
1 answer

How do you add a background thread to flask in Python?

The example below creates a background thread ...READ MORE

answered Nov 19, 2018 in Python by Nymeria
• 3,520 points
0 votes
0 answers

How do you parse a string in python?

After splitting the string, how does parsing ...READ MORE

Jul 8 in Python by Waseem
• 4,470 points
0 votes
1 answer
+1 vote
2 answers

how can i count the items in a list?


answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 337 views
+4 votes
6 answers
0 votes
1 answer

How do I make a delay in Python?

Hahah! Yes, you need to create a ...READ MORE

answered Jul 4 in Python by Pooja
0 votes
1 answer