-1

Is there a way to pass function with arguments that looks clean? For example, if I want pass function h with particular parameter-values a b from ars so that ars1 runs with these particular a b, what should I do? I want to do

def h(x, a, b):
    return x * a * b

def ars(fcn):
    a = 1
    b = 2
    return ars1(fcn( . , a,b))

def ars1(fcn):
    return fcn(1)*fcn(1)

ars(h)

I could do

def h(x, a, b):
    return x * a * b

def ars(fcn):
    a = 1
    b = 2
    return ars1(fcn, a, b)

def ars1(fcn, a, b):
    return fcn(1, a, b)*fcn(1, a, b)

which achieves the purpose but since a b have nothing to do with ars1, the code looks messy and lacks modularity.

ZHU
  • 904
  • 1
  • 11
  • 25
  • 1
    "For example, Creating Python function with partial parameters uses functools which in my opinion not very standard" - you've got some weird opinions. – user2357112 Jul 01 '18 at 22:42
  • 1
    I’d like to say that functools is quite common, with “partial” in particular being very heavily used. It is in the standard library for a reason. That said, lambda is the other way to go, or *args. – soundstripe Jul 01 '18 at 22:43
  • 1
    Using standard library functions should be considered standard. – juanpa.arrivillaga Jul 01 '18 at 23:29

1 Answers1

1

You can use a lambda function to achieve this:

def h(x, a, b):
    return x * a * b

def ars(fcn):
    a = 1
    b = 2
    return ars1(lambda x: fcn(x, a, b))

def ars1(fcn):
    return fcn(1)*fcn(1)

ars(h)
blhsing
  • 91,368
  • 6
  • 71
  • 106