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
🌐
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.
🌐
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.
🌐
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.
Find elsewhere
🌐
LabEx
labex.io › tutorials › python-how-to-create-default-lambda-in-python-462152
How to create default lambda in Python | LabEx
graph TD A[When to Use Lambda Functions] --> B[Short, Simple Operations] A --> C[Function Arguments] A --> D[Functional Programming] A --> E[Temporary Functions] ... Lambda functions are typically slower than regular functions due to their dynamic nature. For performance-critical code, consider using regular function definitions. 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
🌐
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 ().
🌐
YouTube
youtube.com › watch
Code Default Arguments in AWS Lambda Functions - YouTube
Discover why your AWS Lambda costs might be spiralling out of control due to a common Python programming practice! In this comprehensive guide, we dive deep ...
Published   February 11, 2025
🌐
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.
🌐
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.
🌐
Python documentation
docs.python.org › 3 › tutorial › controlflow.html
4. More Control Flow Tools — Python 3.14.4 documentation
It is also possible to define functions with a variable number of arguments. There are three forms, which can be combined. The most useful form is to specify a default value for one or more arguments. This creates a function that can be called with fewer arguments than it is defined to allow.
🌐
Kitchin Research Group
kitchingroup.cheme.cmu.edu › blog › 2013 › 05 › 20 › Lambda-Lambda-Lambda
lambda - The Kitchin Research Group
You can also make arbitrary keyword arguments. Here we make a function that simply returns the kwargs as a dictionary. This feature may be helpful in passing kwargs to other functions. ... Of course, you can combine these options. Here is a function with all the options. f = lambda a, b=4, *args, **kwargs: (a, b, args, kwargs) print f('required', 3, 'optional-positional', g=4)
🌐
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   4 weeks 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 ...