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, 2019 in Python by Isha
1,155 views

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, 2019 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
• 58,020 points
986 views
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,560 points
36,877 views
0 votes
0 answers

How do you parse a string in python?

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

Jul 8, 2019 in Python by Waseem
• 4,540 points
660 views
0 votes
1 answer
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,464 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,125 views
0 votes
1 answer

How do I make a delay in Python?

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

answered Jul 4, 2019 in Python by Pooja
1,312 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP