Lambda's take the same signature as regular functions, and you can give reg a default:
f = lambda X, model, reg=1e3: cost(X, model, reg=reg, sparse=np.random.rand(10,10))
What default you give it depends on what default the cost function has assigned to that same parameter. These defaults are stored on that function in the cost.__defaults__ structure, matching the argument names. It is perhaps easiest to use the inspect.getargspec() function to introspect that info:
from inspect import getargspec
spec = getargspec(cost)
cost_defaults = dict(zip(spec.args[-len(defaults:], spec.defaults))
f = lambda X, model, reg=cost_defaults['reg']: cost(X, model, reg=reg, sparse=np.random.rand(10,10))
Alternatively, you could just pass on any extra keyword argument:
f = lambda X, model, **kw: cost(X, model, sparse=np.random.rand(10,10), **kw)
Answer from Martijn Pieters on Stack OverflowLambda's take the same signature as regular functions, and you can give reg a default:
f = lambda X, model, reg=1e3: cost(X, model, reg=reg, sparse=np.random.rand(10,10))
What default you give it depends on what default the cost function has assigned to that same parameter. These defaults are stored on that function in the cost.__defaults__ structure, matching the argument names. It is perhaps easiest to use the inspect.getargspec() function to introspect that info:
from inspect import getargspec
spec = getargspec(cost)
cost_defaults = dict(zip(spec.args[-len(defaults:], spec.defaults))
f = lambda X, model, reg=cost_defaults['reg']: cost(X, model, reg=reg, sparse=np.random.rand(10,10))
Alternatively, you could just pass on any extra keyword argument:
f = lambda X, model, **kw: cost(X, model, sparse=np.random.rand(10,10), **kw)
have you tried something like
f = lambda X, model, **kw: cost(X, model, sparse = np.random.rand(10,10), **kw)
then reg (and any other named argument you want to pass through (other than sparse)) should work fine.
How to add default/optional parameters for Lambda functions?
can a lambda or other method be used as a default parameter in python - Stack Overflow
python - How to use a lambda as a default argument? - Stack Overflow
python - Default arguments in lambda function - Stack Overflow
I want to do:
var myLambda = (int myNum, string myStr = "default string") => {
DoSomething(myNum);
ProcessString(myStr);
};
myLambda(1, "new string");
myLambda(2);Spoiler: You can't do it because optional/default parameters are not allowed in lambdas, but there is a proposal out for it. Which is a damn shame because I love how sleek lambdas are.
What are the alternative ways achieve this?
Can this be done with lambda ?
No. What you have is syntactically valid, but the default value of curr_time ends up being the lambda function, not the datetime value it would return if it were called. This can be worked around as:
if callable(curr_time):
curr_time = curr_time()
but at that point, you might as well just use the if curr_time is None: approach you included in your question.
(Note that the comparison should be made as is None, not == None.)
You cannot, for a pretty fundamental reason:
def f(arg=expression):
evaluates expression at the time the def statement is executed, and the object expression evaluates to is saved in the function object as the default value of local name arg. When the function body is executed, within the body arg will simply retrieve the object expression originally evaluated to. And nothing about any of that can be changed.
So, sure, you can supply a lambda expression, but then arg will be bound to the function object the lambda expression returns. If you want to call that function object, you'll need to spell that as arg() in the body. Plain old arg will merely retrieve the function object.
According to PEP8, you should "Always use a def statement instead of an assignment statement that binds a lambda expression directly to a name." So, one thing I would change is:
def blank_fn(*args, **kwargs):
pass
However, I think a more pythonic way to do this is:
def perform_task(callback=None):
print 'doing stuff'
if callback is not None:
callback('x', 'y', z='z')
There shouldn't be any need to call a function that does nothing. Truth value testing is cheaper than function calling.
def do_nothing(*args, **kwargs): pass
def do_something(arg, callback=do_nothing):
a = 1 + 2
callback('z', z='z')
def do_something_else(arg, callback=None):
a = 1 + 2
if callback is not None:
callback('z', z='z')
%timeit do_something(3)
1000000 loops, best of 3: 644 ns per loop
%timeit do_something_else(3)
1000000 loops, best of 3: 292 ns per loop
I think the previous answer is superior, as it provides a better way to accomplish what the OP wanted to do.
However there may arguably be circumstances when you want a noop function when testing, or if you are monkey patching something.
So to answer the OP Question as asked, you can use Mock:
In [1]: from mock import Mock
In [2]: blank_fn = Mock(return_value=None)
In [3]: blank_fn()
In [4]: blank_fn("foo")
In [5]: blank_fn(bar="foo")
In [6]: blank_fn("foobar", bar="foo")
In [7]:
The problem is that sometimes want to pass no argument to execute, so I want function to default to the empty function.
Works fine for me:
>>> def execute(function = lambda x: x, *args):
... print function, args
... function(args)
...
>>> execute()
<function <lambda> at 0x01DD1A30> ()
>>>
I do note that your example attempts to use f within the implementation, while you've called the parameter function. Could it really be that simple? ;)
That said, you need to understand that Python will have no way to tell that you want to skip the defaulted argument unless there are no arguments at all, so execute(0) cannot work as it attempts to treat 0 as a function.
An anonymous function returning None is often an appropriate no-op:
def execute(func=lambda *a, **k: None, *args, **kwargs):
return func(*args, **kwargs)