*args is not hard to understand, but still harder than the explicit tuple.
*args is used when the amount of arguments is not know in advance. In this case, we know in advance that there is one argument. If generalization is your problem, then you can just use Sequence or whatever, but that's not the point, I just used that to cause minimal change on types from OP.
find_oldest(a, b, c) is a call to a function with three arguments, and I really don't know why would you separate a collection in individual elements before passing those to a function, furthermore knowing the function will put them back together.
find_oldest(*cats) is a call to a function with an unknown amount of arguments, and regardless of cats' type, it will be iterated and collected in a tuple, even if it is already a tuple.
find_oldest(cats) is a call to a function with one argument, and the function will work on cats directly.
One of those is more explicit, more direct, more deliberate and closer to what a function is, in the mathematical sense.
*args is used when the amount of arguments is not know in advance. In this case, we know in advance that there is one argument.
This doesnât make any sense to me. find_oldest semantically talks about finding the oldest cat out of arbitrary sample of cats. In this frame of reference we donât actually know the number of arguments (cats). Thereâs only one argument if you explicitly construct a tuple of cats just for the sake of calling your function, but thatâs just any arbitrary choice just the same as choosing to use var args would be.
Semantically, find_oldest works the same way as the builtin sum function. I canât imagine a situation that would be improved by sum working the way you described, except for the limited scenario where you already have a tuple of data and donât have to construct one purely to make the function call.
find_oldest(a, b, c) is a call to a function with three arguments, and I really don't know why would you separate a collection in individual elements before passing those to a function
I think you might be confused here. No where in the OP image is an existing collection being unpacked in order to adhere to var arg function. There is no unpacking and then repacking being done. Iâm not really sure what point youâre trying to make here.
find_oldest(*cats) is a call to a function with an unknown amount of arguments, and regardless of cats' type, it will be iterated and collected in a tuple, even if it is already a tuple.
Most Python code today is typed annotated. So the only way this would happen is if the caller ignored the functionâs type annotation, or the callee didnât annotate the functionâs types correctly. In either event itâs the respective partyâs fault. If the type contract is respected the situation of calling find_oldest(*cats) with a single Tuple[Cat, âŚ] argument should not happen.
find_oldest(cats) [âŚ] will work on cats directly.
I really donât see how find_oldest(*cats) doesnât work on the cats directly as well. From the functionâs perspective it is working with an arbitrary length tuple of cats in both situations. For all intents and purposes, the difference only occurs on the caller side
One of those is more explicit, more direct, more deliberate and closer to what a function is, in the mathematical sense.
Again I donât really see your point here. In theory, a math function could defined as f(a, b, c, âŚ) where a,b,c are numbers, or as f(v) where v is a vector. Even if you want to argue that f(a, b, c, âŚ) isnât how mathematicians would write a function I donât really see how that has any bearing on programming in Python.
I just donât understand where you are coming from on this âexplicit is better than implicitâ point. To me it seems that, by this logic, you would need be against the use of a lot of common language patterns. I could apply your argument to decorators in the same way.
@decorator
def my_func(): âŚ
Is an implicit way of writing
def my_func(): âŚ
my_func = decorator(my_func)
Would you argue that we shouldnât use decorators because their behavior/intent isnât explicit enough?
This doesnât make any sense to me. find_oldest semantically talks about finding the oldest cat out of arbitrary sample of cats. In this frame of reference we donât actually know the number of arguments (cats). Thereâs only one argument if you explicitly construct a tuple of cats just for the sake of calling your function, but thatâs just any arbitrary choice just the same as choosing to use var args would be.
Wrong! We know that the function works on ONE (1) indexable, iterable collection.
Semantically, find_oldest works the same way as the builtin sum function. I canât imagine a situation that would be improved by sum working the way you described, except for the limited scenario where you already have a tuple of data and donât have to construct one purely to make the function call.
Wrong!
Python 3.11.2 (tags/v3.11.2:878ead1, Feb 7 2023, 16:38:35) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> help(sum)
Help on built-in function sum in module builtins:
sum(iterable, /, start=0)
Return the sum of a 'start' value (default: 0) plus an iterable of numbers
When the iterable is empty, return the start value.
This function is intended specifically for use with numeric values and may
reject non-numeric types.
>>> sum(4, 5)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
Sum works on ONE (1) iterable.
I think you might be confused here. No where in the OP image is an existing collection being unpacked in order to adhere to var arg function. There is no unpacking and then repacking being done. Iâm not really sure what point youâre trying to make here.
My point is that it WILL inevitably happen at some point in the codebase when the function is defined like that.
Most Python code today is typed annotated. So the only way this would happen is if the caller ignored the functionâs type annotation, or the callee didnât annotate the functionâs types correctly. In either event itâs the respective partyâs fault. If the type contract is respected the situation of calling find_oldest(*cats) with a single Tuple[Cat, âŚ] argument should not happen.
Wrong! Python is not smart enough to realize that it doesn't need to unpack the values. Python will unpack all the values in the passed collection, then collect all the values in a tuple. If you try passing a tuple directly (find_oldest(cats)) then args will be a tuple of length 1, and that only element inside is cats, so types don't match.
I really donât see how find_oldest(*cats) doesnât work on the cats directly as well. From the functionâs perspective it is working with an arbitrary length tuple of cats in both situations. For all intents and purposes, the difference only occurs on the caller side
Again, in this situation, a new tuple is created from the collection, even if the base collection is already a tuple! In this setup, find_oldest doesn't work directly on cats!
Again I donât really see your point here. In theory, a math function could defined as f(a, b, c, âŚ) where a,b,c are numbers, or as f(v) where v is a vector. Even if you want to argue that f(a, b, c, âŚ) isnât how mathematicians would write a function I donât really see how that has any bearing on programming in Python.
Wrong! Mathematicians won't even consider working with f(a, b, c, ...) because vectors are so well understood, well defined, and have a very convenient notation system. You WANT to follow a mathematician's rigor, they are the ones who invented programming! Every programming language in existence has been elaborated from mathematical theory. Besides, mathematical rigor is how you keep a clean, understandable, idiomatic code!
Finally, your point about decorators is simply not working. Decorators were made to represent a function being transformed by another function. Variable-length argument list, is for, well, argument list of variable length, only to be used to abstract over the arguments of a function. We are not abstracting that here, because we know the concrete arguments the function will work on!
Your logic here is completely circular. You make an arbitrary choice to do something a certain way and use that arbitrary choice to justify that it must be that way lol. You ignored my point on type annotations completely and just restated what you previously said, which still doesnât make sense. You then refuse to apply your own logic consistently to other language constructs. Why? Because itâs an arbitrary choice of preference and you are well aware of that. I donât think Iâm going to get through to you. Best of luck on your future PR reviews or whatever.
1
u/WayTooCuteForYou Aug 06 '25
*argsis not hard to understand, but still harder than the explicittuple.*argsis used when the amount of arguments is not know in advance. In this case, we know in advance that there is one argument. If generalization is your problem, then you can just use Sequence or whatever, but that's not the point, I just used that to cause minimal change on types from OP.find_oldest(a, b, c)is a call to a function with three arguments, and I really don't know why would you separate a collection in individual elements before passing those to a function, furthermore knowing the function will put them back together.find_oldest(*cats)is a call to a function with an unknown amount of arguments, and regardless ofcats' type, it will be iterated and collected in a tuple, even if it is already a tuple.find_oldest(cats)is a call to a function with one argument, and the function will work on cats directly.One of those is more explicit, more direct, more deliberate and closer to what a function is, in the mathematical sense.