0

How would one use higher-order functions (functions returning other functions) in Python?

This is my JavaScript example, whose programming concept I would like to use in Python as well. Let's say, for example, that I would like to wrap a string in an HTML element. In JavaScript, it would look like this:

var wrapInElement = function(ele) {
    return function(inp) {
        return "<" + ele + ">" + inp + "</" + ele + ">";
    };
};

And then I would use it like this:

var names = ["Mike", "Tony", "John"];
names.map(wrapInElement("p"));

How could this look like in Python? Is there a way to write functions that return customized functions like in the example above? One could also write a class in Python that accomplishes this task but wouldn't that be too much for such a basic task?

2
  • 2
    have you tried to translate your JS code into Python? have you tried anything at all? Commented Nov 19, 2013 at 9:52
  • Actually, I was so sure it wouldn't work that way that I didn't try what was offered as an solution. I probably should have done that, as one can really translate it one to one. Commented Nov 19, 2013 at 10:09

2 Answers 2

4

In python, functions are first-class citizens, just like in javascript:

def adder(n):
    def add_func(x):
        return x + n
    return add_func

add3 = adder(3)
print map(add3, [1,2,3])  # [4, 5, 6]

This is not the only option, for example adder can also be written as:

def adder(n):
    return lambda x: x + n

Note that functional style is not quite pythonic, when possible, we prefer comprehensions and generators over high-order functions:

wrapped = ["<p>{}</p>".format(name) for name in names]
Sign up to request clarification or add additional context in comments.

Comments

2

Your JavaScript example can be rewritten (arguably more cleanly) using partial application:

// depends on `_` -- see link above
function wrapInElement(ele, inp) {
    return "<" + ele + ">" + inp + "</" + ele + ">";
};

var f = _.partial(wrapInElement, "p")

... then use f as a normal function ...

And pretty much the same way in Python:

from functools import partial

def wrap_in_element(ele, inp):
    return "<%s>%s</%s>" % (ele, inp, ele)

f = partial(wrap_in_element, "p")

... then use f as a normal function ...

While yes, one can use higher-order functions in both Python and JavaScript, if you find yourself returning functions in order to roll-your-own partial application mechanism, you should realize that this ugliness (which appears in both languages' examples) is not necessary thanks to the awesome libraries out there.

1 Comment

Let me please put emphasis on the fact that this solution is even simpler than the one that I marked as the correct answer. I didn't know partial applications, neither in JavaScript nor in Python. This is quite interesting. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.