4 ms·
> Which is redundant for most functions as they only have positional parameters. It would not need to exist if that were the case. The “default” in Python is
by masklinn 2y ago
> Which is redundant for most functions as they only have positional parameters.
It would not need to exist if that were the case.
The “default” in Python is that a parameter can be passed both positionally and by keyword. Until Python 3.8, the only way to have positional only parameters in pure Python was to use *args and unpack them yourself.
- thaumasiotes 2y agoWhy does it need to exist even though that's not the case? What's the advantage of stopping someone from naming the parameter when they pass it in?
- apple1417 2y agoReading the PEP for adding it, it seems primarily concerned with mirroring how the C builtins generally don't allow it, for performance. https://peps.python.org/pep-0570/#rationale https://peps.python.org/pep-0570/#rationale That being said, you can find quite a lot of uses in the modules written in Python - though I'm not going to pretend I can tell you the reasoning for all them. grep "/\s*[,)]" /usr/lib/python3.13/ --recursive --include "*.py" --exclude-dir site-packages In my own code, the main use I've found is that they don't "reserve" a slot in kwargs - this is totally valid code: def func(x, /, **kwargs): print(x, kwargs) func(1, x=2) You could've worked around it by taking a dict before, but it's just a lot friendlier being able to take args as normal.
- rtpg 2y agoI did not know that about kwargs! Very interesting and useful to be kind of pedantic and not have unintentional conflicts at that level
- masklinn 2y ago> What's the advantage of stopping someone from naming the parameter when they pass it in? There are cases where a parameter name is worthless on the calling side, this is especially common for unary or binary functions. In that case, the ability to name the parameter on the call site just constrains the implementation, and the overrides (in the case of methods).
- IgorPartola 2y agoRight. For example, something like max() or abs() have no meaningful parameter names. But for example max() does take a `key` named argument as well.
- ok_dad 2y agoEdit: this is wrong, I was thinking/merging the idea of the '*' into this one, my bad. -- original below -- > What's the advantage of stopping someone from naming the parameter when they pass it in? From what I have seen, this feature is mostly used for the opposite direction, to force the caller to use keyword arguments past a certain number of (or zero) positional arguments.
- thaumasiotes 2y agoBut that's *, not /.
- deleted 2y ago[deleted]
- ok_dad 2y agoyep!
- alkh 2y agoWhen parameter names are meaningless for the final result def abs_days_difference(date1, date2, /): # note that swapping passed params will yield the same result delta = abs((date2 - date1).days) return delta d1 = date(2023, 1, 1) d2 = date(2024, 1, 15) abs_days_difference(d1,d2)==abs_days_difference(d2,d1) The function returns the absolute diff in the number of days, so date1, date2 are meaningless variable names. It looks like then introduce an order(could #1 be the earlier date, while #2 be the later one?). If you don't get users any options it is clear that the order is meaningless Compare with def day_difference(earlier_date, later_date): delta = (later_date - earlier_date).days return delta Note that now d1 = date(2023, 1, 1) d2 = date(2024, 1, 15) day_difference(d1,d2)!=day_difference(d2,d1) If you function signature is day_difference(date1, date2,*) you have to specify params as kwargs only, removing the confusion: d1 = date(2023, 1, 1) d2 = date(2024, 1, 15) #this now doesn't work day_difference(d1,d2) # this works and is much clearer for caller day_difference(earlier_date=d1,later_date=d2)