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 Overflow
Discussions

How to add default/optional parameters for Lambda functions?
I struggle to think of a use case. Lambdas are for short-lived things where context is apparent. Optional parameters are a syntax sugar for method overloading. It's not a concept that lends itself well to lambdas. Barring a good use case I struggle to come up with alternatives because I can't remember ever wanting this. If I saw a good use case we could talk architecture. But in general "an interface with overloaded methods" is the next step of abstraction above "a lambda". More on reddit.com
🌐 r/csharp
9
0
May 24, 2023
can a lambda or other method be used as a default parameter in python - Stack Overflow
I would like to use a default parameter that is initialized when you call the function so its a different default parameter each time you call it. To illustrate what I'm asking say you want a time ... More on stackoverflow.com
🌐 stackoverflow.com
python - How to use a lambda as a default argument? - Stack Overflow
I'm trying to make a function that will take an optional parameter, callback, which must be a function. I'd like the default value for this function to be nothing - i.e. a function that does nothi... More on stackoverflow.com
🌐 stackoverflow.com
python - Default arguments in lambda function - Stack Overflow
But how to give default argument to lambda function without partial, so that () also could run it like (lambda fun:fun(),defaultargument)()? More on stackoverflow.com
🌐 stackoverflow.com
🌐
DataFlair
data-flair.training › blogs › python-lambda-expression
Python Lambda Expression - Declaring Lambda Expression & Its Defaults - DataFlair
March 8, 2021 - But if it’s there in the arguments, either it should have a default value or must be passed as an argument to the call. ... Here, both a and b are missing values. ... The variable a is still missing a value. ... Finally, since no argument here is missing a value, it works just fine. It isn’t mandatory to provide arguments to a lambda expression in Python, it works fine without them.
🌐
DEV Community
dev.to › divshekhar › python-lambda-function-po3
Python Lambda Function - DEV Community
June 19, 2023 - We have called this lambda function and passed two arguments 2 and 3. The lambda function specifies a default value for an argument using default arguments when the caller does not pass the argument.
Find elsewhere
🌐
Bestprog
bestprog.net › en › 2021 › 06 › 17 › python-default-arguments-in-lambda-expressions
Python. Default arguments in lambda expressions | BestProg
June 17, 2021 - If you do not specify the values of the arguments when calling the lambda expression, then they will be assigned the default values value1, …, valueN.
🌐
Stack Overflow
stackoverflow.com › questions › 51439359 › default-arguments-in-lambda-function
python - Default arguments in lambda function - Stack Overflow
import time from functools import partial (lambda fun:fun())(partial(time.sleep,2)) #runs, sleeps 2 seconds partial(lambda fun:fun(),partial(time.sleep,2))() #runs, sleeps 2 seconds · partial was used to give default argument 2 to sleep, which runs as fun(), and was given as an argument to lambda function, which was run by ().
🌐
Pythondoeswhat
pythondoeswhat.com › 2012 › 11 › lambda-default-parameters.html
Python Does What?!?: lambda default parameters
November 12, 2012 - Default parameters are only evaluated once. The flip side of the correctness of the default parameter (lambda a=i: a) is that it has the "gotcha" of mutable types. Everyone gets bit by def foo(bar=[]) at some point.
🌐
LabEx
labex.io › tutorials › python-how-to-create-default-lambda-in-python-462152
How to create default lambda in Python | LabEx
At LabEx, we recommend using lambda functions judiciously, focusing on readability and code maintainability. ## Lambda with default argument multiply = lambda x, y=2: x * y print(multiply(5)) ## Output: 10 print(multiply(5, 3)) ## Output: 15
🌐
GitHub
github.com › python › mypy › issues › 12557
Cannot infer type of lambda when using default arguments · Issue #12557 · python/mypy
April 10, 2022 - from typing import Callable, List def foo(factories: List[Callable[[], int]]): return sum(f() for f in factories) names = ["0", "abc"] # Inference works fine assert foo([lambda: len(name) for name in names]) == 4 # Cannot infer type of lambda assert foo([lambda name=name: len(name) for name in names]) == 4 def foo2(factories: List[Callable[[int], int]]): return sum(f(0) for f in factories) # Inference works fine assert foo2([lambda i: len(name) for name in names]) == 4 # Cannot infer type of lambda assert foo2([lambda i, name=name: len(name) for name in names]) == 4 ... There should be no error. Especially because the other variants without default arguments are deemed fine.
Author   mxmlnkn
🌐
Real Python
realpython.com › python-lambda
How to Use Python Lambda Functions – Real Python
December 1, 2023 - A Python lambda function behaves like a normal function in regard to arguments. Therefore, a lambda parameter can be initialized with a default value: the parameter n takes the outer n as a default value.
🌐
GeeksforGeeks
geeksforgeeks.org › python › default-arguments-in-python
Default arguments in Python - GeeksforGeeks
Example: Here's a simple example that shows how default arguments work. ... In the first call greet(), no argument is passed, so Python uses the default value "Guest".
Published   1 month ago
🌐
The Hitchhiker's Guide to Python
docs.python-guide.org › writing › gotchas
Common Gotchas — The Hitchhiker's Guide to Python
Do not forget, you are passing a list object as the second argument. Sometimes you can specifically “exploit” (read: use as intended) this behavior to maintain state between calls of a function. This is often done when writing a caching function. Another common source of confusion is the way Python binds its variables in closures (or in the surrounding global scope). def create_multipliers(): return [lambda ...
🌐
Kitchin Research Group
kitchingroup.cheme.cmu.edu › blog › 2013 › 05 › 20 › Lambda-Lambda-Lambda
lambda - The Kitchin Research Group
There are many times where you ... in python, and it is inconvenient to have to use def to create a named function. Lambda functions solve this problem. Let us look at some examples. First, we create a lambda function, and assign it to a variable. Then we show that variable is a function, and that we can call it with an argument...
🌐
GitHub
github.com › micropython › micropython › issues › 1709
lambda as default function argument · Issue #1709 · micropython/micropython
December 11, 2015 - lambda as default function argument#1709 · Copy link · Labels · bug · pohmelie · opened · on Dec 11, 2015 · Issue body actions · >>> def yoba(f=lambda: 1): pass ... ERROR: stack size not back to zero; got 1 ERROR: stack size not back to zero; got 1 ERROR: stack size not back to zero; got 1 ·
🌐
Utexas
johnfoster.pge.utexas.edu › numerical-methods-book › PythonIntro_Functions.html
Functions: Argument Types and Lambda Functions
September 8, 2020 - Keyword arguments are defined by assigning a value to the argument, e.g. b=2 in this example. Keyword arguments are optional when calling the function and if not defined, automatically take on the default value that was assigned in the function definition.