python - Why does this lambda require *arg, what difference does it make? -


i came across pretty clever little function takes 2 functions, applies 1 on top of each other given argument x:

def compose(f,g):     return lambda *x: f(g(*x)) 

now issue *x, don't see doing here. why couldn't simple x (without asterisk)?

here tests:

>>> def compose(f,g): ...   return lambda *x: f(g(*x)) ...  >>> = lambda i: i+1 >>> = lambda b: b+1 >>> compose(this,that)(2) 4  >>> def compose(f,g): ...   return lambda x: f(g(x)) ...  >>> compose(this,that)(2) 4  >>> def compose(f,g): ...   return lambda *x: f(g(*x)) ...  >>> compose(this,that)(2,2) typeerror: <lambda>() takes 1 argument (2 given) 

if g (that in tests) can take variable number of arguments, lambda *x: f(g(*x)) can useful.

otherwise, not much.

the aim allow composed function invoked number of arguments, , these arguments passed inner function in composition.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -