Steven D'Aprano
"> ""If param is missing **or None**, the default if blah..."

> The bottom line is:
>
> you *don't actually* want the parameter to default to the value of a
> sentinel.

Yes I do. I *do* want to be able to give a convenient sentinel value in
order to explicitly tell the function "give me the default value"."

If you want the parameter to default to an empty list, it's categorically more explicit to have the parameter default to an empty list. It's a hack to say "I wan't this to default to an empty list" then have it actually default to a sentinel value that you'll replace with an empty list later on. That's the opposite of explicit. If you actually wanted to have the parameter default to a sentinel value, why would you then overwrite that value? This isn't a very complicated point.

Steven D'Aprano
"> you *have* to use that hack because you can't express what you want the
> default to actually be.

The point of having default values is so that the caller doesn't have to
express what the default will actually be. If the caller has to express
that value, it's not a default, it's a passed-in argument."

None of my comment had anything to do with the caller's perspective. I know how optional parameters work. I'm specifically talking about the programmer's ability to map their intent to code and the ability of the would-be reader of the code (NOT CALLER) to decipher that intent.

If my intent is that I want a parameter to default to an empty list, I wouldn't assign it to an object called "monkey_pants" by default then add code to the body of the function to check if it's "monkey_pants".

"But wait", I hear you say, "assigning my optional parameter to 'monkey_pants' isn't a hack because I know will *never* in any conceivable circumstances become a valid argument value!"
"assigning a value to 'monkey_pants' is how I explicitly communicate tell the function "give me the default value"!"

Those are both quotes from you with 'None' replaced with 'monkey_pants'.

Steven D'Aprano
"How is it misleading?"

Your way of having a parameter default to an empty list (or whatever) is by having the parameter default to None. That's how it's misleading. I know how the hack works. I know why the hack works. I know how default parameters work. You can spare trying to explain all that again. What you can't explain is why it's more explicit to have a parameter default to a sentinel value when the actual intent is to have it default to a mutable value. If it were more explicit, then why not replace `def func(x=0)` with `def func(x=SENTINEL): if x is SENTINEL: x = 0`?

I'm at a loss for ways to explain my position any clear than that.
On Thursday, December 2, 2021 at 9:52:28 AM UTC-6 Steven D'Aprano wrote:
On Wed, Dec 01, 2021 at 09:00:50PM -0800, abed...@gmail.com wrote:

> Steven D'Aprano
> ""If param is missing **or None**, the default if blah..."

> The bottom line is:
>
> you *don't actually* want the parameter to default to the value of a
> sentinel.

Yes I do. I *do* want to be able to give a convenient sentinel value in
order to explicitly tell the function "give me the default value".


> you *have* to use that hack because you can't express what you want the
> default to actually be.

The point of having default values is so that the caller doesn't have to
express what the default will actually be. If the caller has to express
that value, it's not a default, it's a passed-in argument.

> You're doing something misleading to work around a shortcoming of the
> language.

How is it misleading? The parameter is explicitly documented as taking
None to have a certain effect. None is behaving here as a convenient,
common special constant to trigger a certain behaviour, no different
than passing (for example) buffering=-1 to open() to trigger a very
complex set of behaviour.

(With buffering=-1, the buffer depends on the platform details, and
whether the file is binary, text, and whether or not it is a tty.)

For historical reasons, probably related to C, the default value for
buffering is -1. But it could have just as easily be None, or the string
"default", or an enumeration, or some builtin constant.


--
Steve
_______________________________________________
Python-ideas mailing list -- python...@python.org
To unsubscribe send an email to python-id...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python...@python.org/message/QGPPY4JLABDSKICIVJQZ5IRWMXMYAOH4/
Code of Conduct: http://python.org/psf/codeofconduct/