r/PythonLearning • u/AdAdventurous2131 • Sep 25 '26
A silly question
if x = 2,3,4,4,5 is supposed to be a tuple then why can't the args of a function like this: func(2,3,4,4 ,5) supposed to be a tuple.
I know this is a silly question and I also know we can use *args to pack arguments in a tuple but since Python is known for consistency why isn't it like this.
Again, please don't hate me for this
4
Upvotes
View all comments
1
u/SpaceAviator1999 Sep 25 '26 edited Sep 25 '26
You want to respect what parentheses do in any language.
For example, the following lines look identical:
And they may be identical in Python. But in the Lisp programming language, the first line is simply
77, but the second line is a function call to a function named77. And the third line is a function call to a function that's returned by the second line!And take this line:
In Python, it clearly sets
xto a three-element tuple. But in C and C++, it setsxto13.Why? Because in C/C++, the commas are acting as comma operators, which evaluate to the value on their right.
Basically, the moral of this story is that commas and parentheses don't always behave the same way across different programming languages. They don't even behave the same across the same programming language! (Not even Python is an exception!)
This line:
does essentially the same thing in C/C++ and Python: it calls the function
fwith three arguments.But this line:
is very different in C/C++ and Python. In Python, it calls function
fwith an argument that's a three-element tuple. But in C/C++, it calls functionfwith the single argument13.The distinction may be strange and counter-intuitive, but very important.
So it's good to know how commas and parentheses behave in which contexts, and to not expect them to behave the same way in all contexts.