
On 2020-04-18 01:32, Dominik Vilsmeier wrote:
On 17.04.20 10:53, Steven D'Aprano wrote:
I think that, as little as I like the original proposal and am not really convinced it is necessary, I think that it is better to have the explicit token (the key/parameter name) on the left, and the implicit token (blank) on the right:
key=
I suspect it may be because we read left-to-right in English and Python, so having the implicit blank come first is mentally like running into a pothole at high speed :-)
For function calls, I think it is much easier to infer the parameter name than the argument name. That's because usually functions are meant to generalize, so their parameter names denote a more general concept. Variable names on the other hand refer to some specific data that's used throughout the program. For example imagine the function
def call(*, phone_number, ...) # here '...' means any number of additional parameters
Then in the code we might have the following variables:
phone_number_jane = '123' phone_number_john = '456'
Using any of these variables to invoke the `call` function, it is pretty obvious which parameter it should be assigned to (for the human reader at least, not the compiler):
call(=phone_number_jane)
If on the other hand the parameter was specified it would be ambiguous which variable to use:
call(phone_number=) # should we call Jane or John?
Now this doesn't relate to the original proposal yet, but imagine this is inside some other function and we happen to have another variable `phone_number` around:
def create_conference_call_with_jane_and_john(phone_number): """`phone_number` will be placed in a conference call with Jane and John."""
phone_number_jane = '123' phone_number_john = '456'
call(phone_number=) # Whom do we call here?
Yes, there is a variable `phone_number` around but why use this specifically? `phone_number_jane` and `phone_number_john` are also phone numbers and `call` only asks for a phone number in general, so it's ambiguous which one to use.
If on the other hand I read `call(=phone_number)` then I know `phone_number` is a phone number and `call` expects one for its `phone_number` parameter so it's pretty clear how the assignment should be made.
Another example:
purchases = load_data('purchases.txt') sales = load_data('sales.txt') data = load_data('user_data.txt')
process(data=) # We have multiple data sets available, so in the face of ambiguity, ... # ... refuse the temptation to guess.
Functions parameters usually represent a concept and arguments then represent the specific data. Often (not always) specific data can be assigned a concept but the other way round is almost always ambiguous.
I disagree. The rule is very simple: use the variable with the matching name. You can have only a name before the '=', but normally you can have an expression of arbitrary complexity after it. IMHO, it's simpler to omit the RHS than to omit the LHS whilst also restricting it to a name.