why * is not like unquote (scheme)

Kaz Kylheku kaz at ashi.footprints.net
Thu Mar 20 11:52:12 EST 2003


"Amir Hadar" <amir at clockwise3d.com> wrote in message news:<b5c8uj$s78$1 at news2.netvision.net.il>...
> I checked a simpler case:
> (1,2,*(3,4))
> and this is also syntax error.
> 
> but I can path parameters to a function as follow:
> 
> def f(a,b):
>      print a,b
> 
> l = ("a","b")
> f(*l)
> 
> So why doesn't it work in all cases?

This syntactic sugar is nothing like the splicing unquote ,@ unquote
in Lisp or Scheme. In a parameter list, it's more like the consing dot
(or the &rest parameter in Common Lisp).

In a function call, it's like apply.

    Lisp                    Python
    (defun f (a &rest b)    def f(a, *b)
    (apply 'g 3 list)       g(3, *list)

Why it doesn't work in all cases is because * is a syntactic sugar
which has an entirely context-dependent meaning. It means whatever the
parser wants it to mean in the context of a given grammar production.

Function application, and trailing parameters, have little to do with
the backquote syntax and unquote. The backquote is a convenient
notation for constructing lists.

In Lisp if you wanted to (ab)use unquote to do the job of apply, you
would also have to use eval:

   (eval `(my-function , at my-list))
 
Each time this is evaluated, it constructs a new variation of the
source code to a function call, and then hands that source code to
eval. I hope you don't do this in your Scheme or Lisp programming
unless there is an excellent reason for it! ;)

Unlike the backquote approach, the apply function does not generate a
new function call with varying parameters by rewriting source code; it
just does whatever it takes to split a list into individual parameters
to be passed to the function. This has nothing to do at all with
splicing extra arguments into the function call *syntax* itself.




More information about the Python-list mailing list