This is the most Pythonic way:

def foo(arg=None):
    if arg is None:
        arg = bar()
    ...

If you want the function bar to be called only once, and you don't want it to be called at import time, then you will have to maintain that state somewhere. Perhaps in a callable class:

class Foo:

    def __init__(self):
        self.default_arg = None

    def __call__(self, arg=None):
        if arg is None:
            if self.default_arg is None:
                self.default_arg = bar()
            arg = self.default_arg
        ...

foo = Foo()
Answer from wim 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
🌐
Python Tutorial
pythontutorial.net › home › python basics › python default parameters
Python Default Parameters
October 5, 2020 - 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. However, if you don’t pass the argument, the function will use the default value. To use default parameters, you need to place parameters ...
Discussions

python - Function call as a default function argument - Stack Overflow
Find centralized, trusted content ... you use most. Learn more about Collectives ... Save this question. Show activity on this post. ... but I'm not sure if it's the best way and now, when I am stuck with this, I can't find any info on how to deal with this behaviour. What is the correct way to have function call as a default function argument in python... More on stackoverflow.com
🌐 stackoverflow.com
python - Calling a function with default value for arguments - Stack Overflow
In Python, how can I call a function using the default value of an argument? I know it can be done just not passing the parameter when you call the function, but I would prefer to pass the variable More on stackoverflow.com
🌐 stackoverflow.com
February 21, 2019
How to use default values and arbitrary arguments at one function call in Python? - Stack Overflow
Mixing parameters with and without default values can indeed be confusing. Parameters are the names used in the function definition, arguments are the values passed into a call. When calling, Python will always fill all parameters from positional arguments, including names with default values. More on stackoverflow.com
🌐 stackoverflow.com
optionally using default parameters when calling a function
I remember encountering this problem multiple times and giving up every time. But I just realized that we can play around with kwargs. d = dict(myarg=x, myarg2=x2) # Set up call args. d = {k:v for k,v in d.items() if v is not None} # Filter out all Nones. y = f(**d) So this becomes more convenient as the number of args increases. The number of cases goes up exponentially with args but the code above stays the same. If possible you could also just receive kwargs from your own function and then pass them along. But I understand that it makes the function signature less helpful. def myfunc(**kwargs): f(**kwargs) More on reddit.com
🌐 r/learnpython
16
6
July 31, 2023
🌐
Stack Abuse
stackabuse.com › default-arguments-in-python-functions
Default Arguments in Python Functions
March 16, 2020 - Functions in Python are used to implement logic that you want to execute repeatedly at different places in your code. You can pass data to these functions via function arguments. In addition to passing arguments to functions via a function call, you can also set default argument values in Python functions.
🌐
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:
🌐
BeginnersBook
beginnersbook.com › 2019 › 06 › python-function-arguments-default-keyword-and-arbitrary
Python Function Arguments – Default, Keyword and Arbitrary
June 14, 2019 - We can provide default values to the function arguments. Once we provide a default value to a function argument, the argument becomes optional during the function call, which means it is not mandatory to pass that argument during function call. Let’s take an example to understand this concept.
🌐
TutorialsPoint
tutorialspoint.com › What-are-default-arguments-in-python
Python - Default Arguments
June 12, 2020 - Here, the second call to the function will not pass value to "city" argument, hence its default value "Hyderabad" will be used. # Function definition def showinfo( name, city = "Hyderabad" ): "This prints a passed info into this function" print ("Name:", name) print ("City:", city) return # Now call showinfo function showinfo(name = "Ansh", city = "Delhi") showinfo(name = "Shrey") ... Let us look at another example that assigns default value to a function argument.
Find elsewhere
🌐
Programiz
programiz.com › python-programming › function-argument
Python Function Arguments (With Examples)
No value is passed during the function call. Hence, default value is used for both parameters a and b.
Top answer
1 of 2
7

Mixing parameters with and without default values can indeed be confusing. Parameters are the names used in the function definition, arguments are the values passed into a call.

When calling, Python will always fill all parameters from positional arguments, including names with default values. size is just another parameter here, even though it has a default value. You can also use name=value syntax in a call to assign an argument value to a specific parameter (whether or not they have a default value). But you can't tell Python not to assign something to size, not with your current function definition, because everything before the *toppings parameter is always going to be a regular positional parameter.

The *toppings parameter will only capture any positional arguments after all the other parameters have received values. So 'onion' is assigned to size, and the remainder is assigned to *toppings.

In Python 3, you can make size a keyword-only parameter, by placing them as name=default after the *toppings name, or an empty *:

def make_pizza(*toppings, size=15):

Now size can only be set from a call with size=<new value> keyword argument syntax.

In Python 2, you can only capture such keyword arguments with a **kwargs catch-all parameter, after which you need to look into that dictionary for your size:

def make_pizza(*toppings, **kwargs):
    size = kwargs.get('size', 15)  # set a default if missing

In both cases, you have to remember to explicitly name size, and put such explicitly named keyword arguments after the positional arguments:

make_pizza('ham', 'extra meat', 'sweet con', 'pepperoni', size=17)
2 of 2
3

There's a fundamental problem with your approach: It's ambiguous because how do you know if you intended it as size or as topping? Python can't do that so you need to find an alternative.

Well, you could simply remove size from the argument list and interpret the first *toppings argument as size if it's an integer:

def make_pizza(*toppings):
    if toppings and isinstance(toppings[0], int):
        size, toppings = toppings[0], toppings[1:]
    else:
        size = 15
    print("\nMaking a {}-inch pizza with the following toppings: ".format(size))
    for topping in toppings:
        print("- " + topping)

However that will fail for cases where a simple type check isn't possible. Maybe the better approach would be to make toppings a normal argument and size an optional argument:

def make_pizza(toppings, size=15):
    print("\nMaking a {}-inch pizza with the following toppings: ".format(size))
    for topping in toppings:
        print("- " + topping)

However then you need to pass in a sequence for toppings and it changes the order of toppings and size but it's probably a lot cleaner.

make_pizza(['onion','shrimp','goda cheese','mushroom'])
make_pizza(['ham','extra meat','sweet con','pepperoni'], 17)

You could also keep the toppings as arbitary positional arguments and make size a keyword-only parameter with default (Python3-only):

def make_pizza(*toppings, size=15):
    print("\nMaking a {}-inch pizza with the following toppings: ".format(size))
    for topping in toppings:
        print("- " + topping)

make_pizza('onion','shrimp','goda cheese','mushroom')
make_pizza('ham','extra meat','sweet con','pepperoni', size=17)
🌐
Note.nkmk.me
note.nkmk.me › home › python
Default Parameter Values in Python Functions | note.nkmk.me
May 12, 2025 - If the corresponding argument is omitted during the function call, the default value will be used. def func_default(arg1, arg2='default_x', arg3='default_y'): print(arg1) print(arg2) print(arg3) func_default('a') # a # default_x # default_y ...
🌐
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. For example: ... If you call greet() without any argument, it will print “Hello, Guest”. If you call greet(“Varad”), it will print “Hello, ...
🌐
Proglab
sp.proglab.nl › python › en › default-arguments
Default-arguments - Scientific Programming
If the function is called without defining a value for that specific parameter, the parameter will get its default argument value. Parameters are defined by the names that appear in a function definition, whereas arguments are the values actually passed to a function when calling it.
🌐
Built In
builtin.com › software-engineering-perspectives › arguments-in-python
5 Types of Python Function Arguments | Built In
| Video: Telusko · Default arguments are values that are provided while defining functions. The assignment operator = is used to assign a default value to the argument. Default arguments become optional during the function calls.
🌐
Python Helper
pythonhelper.com › home › python default arguments
Default Arguments In Python with examples
August 9, 2023 - In the first function call, only the name argument is provided, so the default value for message is used, resulting in the output: Hello, Alice!In the second function call, both name and message arguments are provided, so the default value for ...
🌐
Boot.dev
boot.dev › lessons › 3963019a-a8ce-45c0-b988-3e6e4121aa36
Learn Python for Beginners: Default Values | Boot.dev
They're created by using the assignment (=) operator inside the function signature: def get_greeting(email, name="there"): print(f"Hello {name}, welcome! You've registered your email: {email}") If we call it with both parameters, it uses the ...
🌐
Medium
medium.com › @wvrushali27 › function-with-default-arguements-in-python-78afd929b599
Function with default arguements in python | by Vrushali Walekar | Medium
January 2, 2024 - In the case of calling a function containing non-keyword arguments, the order is important. ... In the first call, there is only one required argument and the rest arguments use the default values.
🌐
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 (=).
🌐
Reddit
reddit.com › r/learnpython › optionally using default parameters when calling a function
r/learnpython on Reddit: optionally using default parameters when calling a function
July 31, 2023 -

Hi is there a way of saying if variable x as a certain value then call a function using an arguments default value else call it using a set value.

So is there a better way of doing

if x == None:
   y = f()
else:
  y=f(myarg=x)

I've tried googling it but just get pages upon pages of how to define function to use default values.

EDIT: While I appreciates the responses given so far, it seems I've simplified the example too much.

The actual function is a call to a third party module, which as a number of optional arguments, many of which expect objects returned from other function calls, some of which may capture state when defined .

Some of the optional arguments do not accept "None" as a legitimate value.

I wasn't looking for where to put the if: statement I was hoping there was a bit of syntactical sugar to say use the default value Which I guess isn't the case.

🌐
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 - The doc is saying if you use a mutable object as a default argument it will be evaluated only once. So in the previous example: When the function is defined for the first time(I am not saying when it’s called, I’m saying when it’s defined), Python evaluates the default argument and ...