-1

I know you can call a python function like this:

func(x,y,z) 

but can you call a function like this?

func(x)(y)
martineau
  • 119,623
  • 25
  • 170
  • 301
Hugo Reyes
  • 83
  • 4
  • 4
    Yes, if `func(x)` returns a callable. – keepAlive Nov 15 '18 at 23:24
  • Think of it as `a = func(x); a(y)` and/or `(func(x))(y)` - that is, evaluation is left-to-right where each call is made upon a *different* callable expression. Python does not implicitly support partial application / partial functions; the original function must be modified for this emulated form. (There [are functions to wrap an existing function as a partial function](https://www.learnpython.org/en/Partial_functions), but that's not intrinsic to Python and the rules above still apply.) – user2864740 Nov 15 '18 at 23:27
  • Are you asking if you can bind the parameter `x` to the function, and then call it with the additional parameter `y`? If so look at functools.partial(). – soundstripe Nov 15 '18 at 23:52
  • Possible duplicate of [Currying decorator in python](https://stackoverflow.com/questions/9458271/currying-decorator-in-python) – Olivier Melançon Nov 15 '18 at 23:52

2 Answers2

3

Yes, if the function returns a function object. For example:

def a(x):
    def b(y):
        return x + y
    return b
print(a(2)(3))

outputs: 5

blhsing
  • 91,368
  • 6
  • 71
  • 106
-1

This is only useful if you are creating sub routines

a silly example:

a = lambda x,y: lambda z: x+y+z
a(1,1)(3)
5

Well the example does not make sense. To make sense of this we can do:

 a = lambda x:lambda y:y**(1/x)
>>> a(2)(4)
2.0
>>> a(2)(16)
4.0
>>> sqrt = a(2)
>>> sqrt(4)
2.0
>>> cubert = a(3)
>>> cubert(8)
2.0
Onyambu
  • 67,392
  • 3
  • 24
  • 53