You can do the following:

def myF(a, b=None):
    if b is None:
        b = a - 1
    return a * b - 2 * b
Answer from Selcuk on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › default-arguments-in-python
Default arguments in Python - GeeksforGeeks
In the first call greet(), no argument is passed, so Python uses the default value "Guest". In the second call greet("Kate"), the default value is overridden with "Kate". def function_name(param1=value1, param2=value2, ...): # function body
Published: March 18, 2026
🌐
W3Schools
w3schools.com › python › gloss_python_function_default_parameter.asp
Python Default Parameter Value
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Training ... The following example shows how to use a default parameter value. If we call the function without argument, it uses the default value:
Discussions

Python: default value of function as a function argument - Stack Overflow
def myF(a, b): return a*b-2*b Let's say that I want a default value for b to be a-1: def myF(a, b=a-1): return a*b-2*b gets the error message: NameError: name 'a' is not defined I can us... More on stackoverflow.com
🌐 stackoverflow.com
Python Function Default Values
def defaults(foo : str = “Hello”, bar : str = “World”) -> None: print(f”{foo} {bar}”) I believe this is the run down defaults() >>> Hello World defaults(“foo”) >>> foo World defaults( “foo”, “bar”) >>> foo bar defaults(bar = “bar”) >>> Hello bar defaults(bar = “foo”, foo = “bar”) >>> bar foo #also unexpectedly defaults(1,2) >>> 1 2 defaults(foo = “foo”, “bar”) >>>ERROR kwargs must come after args #these can cause errors if not formatted correct only use when necessary defaults([“foo”, “bar”]) >>>> [“foo”, “bar”] World defaults(*[“foo”, “bar”]) >>>> foo bar _dict = {“bar” : “_bar”, “foo” : “_foo”} defaults(**_dict) >>>> _foo _bar If you want to check types def type_default(foo : str = “Hello”, bar : str = None) -> None: if bar: print(f”{foo} {bar}”) else: print(f”{foo} World”) This should check if you accidentally send an false statement (like None, any empty string, list or dictionary, or any number != 0) to it. As you see Nonetype prints “None” in Python, and empty string would print no character, an empty list [], {} but we check all of that here. I like the type hinting but realize that’s just a comment not a requirement in Python. More on reddit.com
🌐 r/learnpython
6
0
June 28, 2023
Today I re-learned: Python function default arguments are retained between executions
I had to find this out the hard way :/ P.s. I am a little rusty on python, but if you want a mutable default value (say a list) you can make the default None then inside the function you would reassign the variable if it's None. More on reddit.com
🌐 r/Python
170
391
January 20, 2023
What the default arguments are?
Some sources point out that arguments might be default. But they are not clear in what these arguments are. Sometimes it looks like the default arguments are mismatched with keyword-only parameters or positional and keyw… More on discuss.python.org
🌐 discuss.python.org
8
0
October 3, 2023
🌐
Python Tutorial
pythontutorial.net › home › python basics › python default parameters
Python Default Parameters
October 5, 2020 - In this syntax, you specify default values (value2, value3, …) for each parameter using the assignment operator (=). When you call a function and pass an argument to the parameter that has a default value, the function will use that argument instead of the default value.
🌐
Reddit
reddit.com › r/learnpython › python function default values
r/learnpython on Reddit: Python Function Default Values
June 28, 2023 -

Say I have a function defined like

def f(foo: str, bar: str | None = "world"):
  print(f"{foo} {bar})
s = None
f("hello") # prints "hello world"
f("hello", s) # prints "hello None"

Is there a data type (or concept) in Python that would enable the second function call to print "hello world" as well? In other words, bar should take on its default value. In javascript, passing undefined for bar would accomplish this. In Python, it seems like I would have to add a conditional to the function.

def f(foo: str, bar: str | None = "world"):
  bar = bar or "world"
  print(f"{foo} {bar})

This isn't a major issue for me, but when a function has a lot of optional arguments with default values, it can be pretty verbose (for Python standards) to add a conditional for every optional argument.

🌐
Codecademy
codecademy.com › learn › learn-intermediate-python-3 › modules › int-python-function-arguments › cheatsheet
Learn Intermediate Python 3: Function Arguments Cheatsheet | Codecademy
If the function is invoked without a value for a specific argument, the default value will be used. ... In Python, a default parameter is defined with a fallback value as a default argument. Such parameters are optional during a function call.
Find elsewhere
🌐
Reddit
reddit.com › r/python › today i re-learned: python function default arguments are retained between executions
r/Python on Reddit: Today I re-learned: Python function default arguments are retained between executions
January 20, 2023 - Well this is easy, it results in a logically wrong value with Python. It means the default value is set to datetime.now at the time the DEF is evaluated, so by the time your function is called that parameters default value is never "now".
🌐
Note.nkmk.me
note.nkmk.me › home › python
Default Parameter Values in Python Functions | note.nkmk.me
May 12, 2025 - You can set default values in a function definition using the format parameter_name=default_value.
🌐
Home and Learn
homeandlearn.uk › python-functions-defaults.html
Python Default function values
You can set up your Python functions to have default values. Between the round brackets of your function definition, come up with a variable name then type an equals sign.After the equals sign, type a value for your variable. For example, suppose we want our function from the previous lesson ...
🌐
TutorialsPoint
tutorialspoint.com › What-are-default-arguments-in-python
Python - Default Arguments
June 12, 2020 - If any value is passed, the default value is overridden with the actual value passed. Default arguments in Python are the function arguments that will be used if no arguments are passed to the function call.
🌐
Codingem
codingem.com › home › python default arguments: a complete guide (with examples)
Python Default Arguments: A Complete Guide (with Examples)
November 5, 2022 - And if the input value is provided, that’s going to be used in the function. You can add default function parameters in Python by giving a default value for a parameter in the function definition using the assignment operator (=).
🌐
Python.org
discuss.python.org › python help
What the default arguments are? - Python Help - Discussions on Python.org
October 3, 2023 - Some sources point out that arguments might be default. But they are not clear in what these arguments are. Sometimes it looks like the default arguments are mismatched with keyword-only parameters or positional and keyword parameters. E.g.: def greet(name, greeting="Hello"): print(f"{greeting}, {name}!") greet("Alice") greet("Bob", "Hi") One source even suggests that the value of greeting (i.e. “Hello”) is that default argument.
🌐
InterServer
interserver.net › home › python › how to use default arguments in python for clean and flexible code
How to Use Default Arguments in Python for Clean and Flexible Code - Interserver Tips
April 23, 2026 - This helps make functions flexible and easier to use. In Python, you can set a default value by using an equal sign = after the parameter name.
🌐
Python.org
discuss.python.org › ideas
A way to have the default value for a parameter as the default of another function: the Default singleton - Ideas - Discussions on Python.org
September 7, 2022 - Often, I use the defaults of another function in my new function or class. For example: def mysorted(it, key=None, reversed=False): # some code return sorted(it, key=key, reversed=reversed) The majority of time…
Top answer
1 of 8
175

Python3.x

In a python3.x world, you should probably use a Signature object:

import inspect

def get_default_args(func):
    signature = inspect.signature(func)
    return {
        k: v.default
        for k, v in signature.parameters.items()
        if v.default is not inspect.Parameter.empty
    }

Python2.x (old answer)

The args/defaults can be combined as:

import inspect
a = inspect.getargspec(eat_dog)
zip(a.args[-len(a.defaults):],a.defaults)

Here a.args[-len(a.defaults):] are the arguments with defaults values and obviously a.defaults are the corresponding default values.

You could even pass the output of zip to the dict constructor and create a mapping suitable for keyword unpacking.


looking at the docs, this solution will only work on python2.6 or newer since I assume that inspect.getargspec returns a named tuple. Earlier versions returned a regular tuple, but it would be very easy to modify accordingly. Here's a version which works with older (and newer) versions:

import inspect
def get_default_args(func):
    """
    returns a dictionary of arg_name:default_values for the input function
    """
    args, varargs, keywords, defaults = inspect.getargspec(func)
    return dict(zip(args[-len(defaults):], defaults))

Come to think of it:

    return dict(zip(reversed(args), reversed(defaults)))

would also work and may be more intuitive to some people.


2 of 8
17

To those looking for a version to grab a specific default parameter with mgilson's answer.

value = signature(my_func).parameters['param_name'].default

Here's a full working version, done in Python 3.8.2

from inspect import signature

def my_func(a, b, c, param_name='apple'):
    pass

value = signature(my_func).parameters['param_name'].default

print(value == 'apple') # True
🌐
Stack Abuse
stackabuse.com › default-arguments-in-python-functions
Default Arguments in Python Functions
March 16, 2020 - Since we did not pass any values for the function arguments, the default argument values, i.e 2 and 4, have been added together. Let's now pass two of our own values to the add_ints() function: ... As expected, 4 and 8 were added together to return 12. A Python function can have both normal ...
🌐
Programiz
programiz.com › python-programming › function-argument
Python Function Arguments (With Examples)
So, according to the positional argument 2 is assigned to argument a, and the default value is used for parameter b. ... No value is passed during the function call.
🌐
The Python Coding Book
thepythoncodingbook.com › home › blog › optional arguments with default values in python functions [intermediate python functions series #3]
Optional Arguments with Default Values in Python Functions
January 18, 2023 - You said you’ll provide two arguments when you defined the function but only provided one. Python can’t live with that, understandably. There’s missing information. Therefore, it cannot execute the function. However, you can define the function so that the parameter number has a default value.
🌐
Medium
medium.com › @nebiyuelias1 › be-careful-when-using-default-arguments-in-python-fd92df94efee
Be careful when using default arguments in Python | by Nebiyu Elias | Medium
December 16, 2021 - I hope Python won’t say “Gotcha” to you after reading this article. So I was using a function with a default argument like this: The pause_item function (I should say method since the code is actually defined inside a class, but it’s OK for the time being) has two default arguments, user is given a default value of None and paused_at is given a default value of timezone.now().