1

In Python, what value can a variable take, so that when a function is invoked with the variable as an argument, the function uses its default value for the parameter instead?

Consider the following code:

def foo(a=100):
    print(a)

b = None #blank value
foo(b)

Desired output:
100

Actual output:
None

I hypothesized that None would work, but clearly it doesn't. What value can I choose for b, so that foo(b) is equivalent to foo()? Or is this simply not possible? I'm in a situation where the value for b can either be defined, or I would like to use the default value of the parameter.

8
  • you could do foo(100) Commented Jul 21, 2022 at 11:35
  • I would like to be able to change the default value only one place and for it to still work Commented Jul 21, 2022 at 11:38
  • 2
    But you're not using the default, you're actively telling it to use a different value instead of the default. Commented Jul 21, 2022 at 11:39
  • 1
    You could do something like def foo(a=None) and then in the first line of the function: if a is None: a=100 Commented Jul 21, 2022 at 11:43
  • to use the default value, call the function without passing the arg, e.g. foo() Commented Jul 21, 2022 at 11:43

4 Answers 4

4

(This answer assumes that you cannot modify foo, and that you cannot use reflection or introspection to determine what the default argument value is.)


It's the absence of an argument, not any particular value used as an argument, that triggers the use of the default value. The only way you can produce nothing out of something is to unpack an empty mapping

foo(**{})

or an empty sequence

foo(*())

Both * and ** are part of the function-call syntax, though, not part of the argument value, so with a variable, it still looks like

b = {}
foo(**b)

b = ()
foo(*b)
Sign up to request clarification or add additional context in comments.

Comments

2

If you want None to revert to a default value, the easiest way is to do the logic in the function itself.

def foo(a=None):
    if a is None:
        a = 100
    print(a)

1 Comment

Better to keep a=100 in function params to keep showing the intention of the function (and also keep your additional code).
1

Function default parameter uses when there don't pass any parameter for the argument. And None is not a blank value. None is an object of NoneType Datatype in python similar to Other Datatype Object.

Instead, you can use

def foo(a=100):
    a=100 if a is None else a //Ternary operator 
    print(a)

b = None #blank value
foo(b)

Output: 100

Comments

0

In my case, I ended up using the inspect module to create a helper function which extracts the default values of the function as described here:

import inspect

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


def foo(a=100):
    print(a)

b = get_defaults(foo)['a']
foo(b)

Output: 100

3 Comments

May I ask why you did this? That's an awful lot of work to do something equivalent to simply not passing the value at all. It works, sure, but so does making a dict and only adding the mapping for "a" if you have a non-default, then unconditionally doing foo(**mydict). I suspect an XY problem.
@ShadowRanger Making a dict with all the parameters and unpacking it is probably a more elegant solution, but it does turn a single line function call into multiple lines of creating an argument dict, checking for which arguments should be added (we're talking upwards of 20), and finally the function call itself, with unpacking arguably being a less familiar operation to novice python developers.
I'll grant you it's a multiline thing, but then, any reasonable case for "do I want to use the default or not?" is also going to be multiline. I won't grant the unpacking argument; it's a key skill to learn, more important than inspect-based reflection code, and novice Python developers that want to become even journeymen need to learn it at some point.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.