Why does the `default` parameter in `json.dump` have that name?

I understand that the default argument in json.dump is a function that is called when the default json encoder can’t encode a certain object. This name just doesn’t make any sense to me. I would think default is the first thing you do, not the LAST thing you do if nothing else works. Also is there a non-default encoder that can be specific in addition to default? I think fallback_encoder or similar would have been much more clear.

I just want to understand a little historical story that I can use to replace my frustration every time I have to use this parameter :slight_smile:.

Not sure in what context you believe “default” to mean the first thing you do. It literally means the last thing you do if nothing else works in all relevant scenarios I can think of. BTW I first learned this word when I learned the switch-case statement in C, where default is the keyword to a block of code that is executed when nothing else matches.

1 Like

Your edit saved that post :).

Come on, think about python functions. The “default” argument is what is used when the caller doesn’t provide an input. It is the first thing that is used unless otherwise specified.

This is a useful bit of historical info of the type I’m looking for. I didn’t learn C so I don’t have this specific background. I will check that out. There’s a good chance I’ll wonder why it was named that way there though.

I look at default value for a parameter as a way to say “try to use a caller-supplied value first, but if the caller doesn’t supply a value, resort to using this value as a fallback” so it’s in line with its meaning elsewhere.

Alternatively, you could think of it this way: the default is the last thing Python falls back on when nothing else is provided.

Picture it like a pipeline:

call() -> check user input -> if missing -> use default

It can’t really be the first thing Python uses, because it only comes into play when nothing else is there.

If you flip it around and make the default first, you’d get:

call() -> use default -> user input never gets checked
1 Like

In typical python functions the parameters may have a default existing already. The user can provide something to override that default.

In json.dump the user is now providing something called default, they’re not providing something the can override the default. That exists in the json.dump function.

It definitely feels like a different semantics function parameter default values which is where I most often use the word “default” when coding.

The switch... default pattern from C gives the best justification. Basically there is a default default function, and the user can override the default default function by passing a function into the default parameter. I’m hoping others can see that this is a bit confusing.

I still strongly think fallback_encoder would have been a less confusing name.

It’s best to think of “default” in computing as “use this as the default if nothing else is provided”.

2 Likes

But in a typical python function, the author of the function writes the signature and they can provide the default argument. The user can then provide a non-default argument.

In the case of json.dump the function author writes the non-default behavior inside the function source code and the user provides somehow the default argument. This is reversed. Also, I can’t see anywhere the “non-default” behavior that the function author wrote except in the source code.

This is just a weird usage of default. Not to mention the name of the function argument gives you no answer to the question “default what?”.

  • There’s a little bit of justification from the switch... default pattern from C, but python isn’t C and, as a python user, I didn’t know about that pattern. It also requires me to explicitly think of json.dump as doing a swtich... default pattern under the hood.
  • The user/author non-default/default paradigm is switched in this function
  • The parameter name doesn’t indicate what it is the default for (the answer is the encoder).
  • fallback_encoder would have been a more descriptive name

This post is essentially “The default parameter in json.dump is badly named, change my mind”. My mind is definitely not changed so far but I’ve learned a little bit. So thanks everyone for the input.

FWIW I kinda agree that it’s not an ideal name, and “converter” might be better. But it’s a marginal gain, not worth changing it now.