Re: passing/catching a variable number of args in python

Guido van Rossum (Guido.van.Rossum@cwi.nl)
Sat, 07 Dec 91 12:43:41 +0100

>You could support calling f(x) by using a separate kind of function call,
>analogous to Lisp apply. f(x) as compared with f(1, 2, 3) really does
>a different thing internally, and it was confusing to me as a
>new user to discover that these were intended to have the same result.

Erm, I don't understand what you mean by "really does a different
thing internally". In my mind they do very different things. In the
second case f is called with the expression-list "1, 2, 3" as
argument, which has the same value as x (which was assigned "1, 2, 3"
a line earlier). Maybe you can explain what you felt again?

I believe that the equivalence is actually quite useful, and used
often enough that a special "apply" function would cripple the
language. Take, for instance, a simple "plot" function that takes a
point as argument. I can either write plot(x, y) or plot(p), assuming
p is a tuple of "x, y" coordinates, and I don't have to know whether
the function was declared with one (point) or with two (coordinate)
arguments. Using your proposal (if I understand it well), I would
have to write f(p[0], p[1]) if it was declared as f(x, y), or I would
have to call it as f((x, y)) if it was declared as f(p). Currently,
in Python, there is an informal rule that says "placing extra
parentheses never hurts".

Of course, all this makes Python's argument passing *very* different
from other languages (except ABC), which may be the reason why so many
people have difficulties with it -- either initially, or eternally.
The eternally case worries me most, if it exists, but the initially
case is also a reason to worry, since it makes Python harder to learn,
whicl easy-to-learn is one of its big advantages.

*** Question to the general audience: is this serious enough to
warrant changing the language (and potentially lots of existing code)?

>Maybe you want to consider calling f with arguments being the elements
>of the tuple x using the syntax "f x". Then "f (1, 2, 3)" is really
>the same kind of function call as "f x". This may be inconsistent with other
>aspects of the Python syntax; I dont have my grammar handy.

This was used in ABC, but it would be inconsistent in Python (assuming
no other syntax changes). For example, it would render x[y] ambiguous:
is it a subscription of list/table x with index/key y, or is it a call
of function x with argument [y] (a list containing one element, y)?
Also, function calls without arguments would be impossible -- just "f"
doesn't call the function f but yields the function object f. (This
isn't a problem in ABC since ABC has no function variables.)

--Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
"It's funny, isn't it? How your best friend can just blow up like that?
I mean, you wouldn't think it was medically possible, would you?"