one-element tuples [Was: Most probably a stupid question, but I still want to ask]

Chris Angelico rosuav at gmail.com
Sun Apr 10 20:38:33 EDT 2016


On Mon, Apr 11, 2016 at 10:33 AM, MRAB <python at mrabarnett.plus.com> wrote:
> For example, object are passed into a function thus:
>
>     f(x, y)
>
> (In reality, it's making a tuple and then passing that in.)

Actually that's not the case; certain syntactic constructs allow you
to specify multiple of something, without packaging them up into a
tuple. Function arguments and parameters are like this; otherwise
keyword arguments would have to be some kind of magic syntax in
tuples, instead of being a feature of function calls.

But there are plenty of situations where what you're describing _is_
the case, such as assigning multiple values to multiple targets:

# Package up y and x into a tuple
# Then unpack the tuple into two names
x, y = y, x

ChrisA



More information about the Python-list mailing list