why * is not like unquote (scheme)

Peter Hansen peter at engcorp.com
Thu Mar 20 12:52:29 CET 2003


Amir Hadar wrote:
> 
> I'me tring to create a factory function that create a class as follow:
> 
> def CreatePreviewFrame(*addins):
>     class tmp(cPreviewFrame,*addins):
>         pass
>     return tmp

It would seem the problem merely lies in the incorrect construction
of the "tmp" class.  You have written this definition:

  class tmp(cPreviewFrame, *addins): 

as though it were a function call, which it is not.  It's a
statement, introduced by the "class" keyword, which happens
to use parentheses to help with the syntax.  The *addins
does not get expanded in this case and the compiler reports
a syntax error.  Perhaps this should be added to the language,
but so far it has not been.

Note that your other experiment with (1, 2, *(3, 4)) is also
not currently supported, again because there's no function call
involved, with the associated expansion of the tuple into a
list of arguments.  Again, no technical reason it couldn't
be added to the language (well, not a major one, I believe)
and it has been discussed before as a possibly desirable change
(meaning nobody has tried to submit a patch yet ;-), but
it is not currently supported.

Maybe this is a rare case where you'll need to use exec to
get the job done right.

-Peter




More information about the Python-list mailing list