[Tutor] Is this a job for zip(), or some other way?

Karl Pflästerer sigurd at 12move.de
Sun Mar 28 13:51:06 EST 2004


On 28 Mar 2004, Isr Gish <- isrgish at fastem.com wrote:

> Where does this * thing come from I don,t remmember seeing it in the docs.

You can find it e.g. in the Python library reference (section calls)

,----
| Calls
| -----
| 
| A call calls a callable object (e.g., a function) with a possibly empty
| series of arguments:
| 
| `call `primary' "(" [`argument_list' [","]] ")"'
| 
| `argument_list `positional_arguments' ["," `keyword_arguments']'
| 
| `                      ["," "*" `expression']'
| 
| `                      ["," "**" `expression']'
| 
| ` | `keyword_arguments' ["," "*" `expression']'
| 
| `                     ["," "**" `expression']'
| 
| ` | "*" `expression' ["," "**" `expression']'
| 
| ` | "**" `expression''
| 
| `positional_arguments `expression' ("," `expression')*'
| 
| `keyword_arguments `keyword_item' ("," `keyword_item')*'
| 
| `keyword_item `identifier' "=" `expression''
| A trailing comma may be present after an argument list but does not
| affect the semantics.
|
| [...]
|
| If the syntax `*expression' appears in the function call, `expression'
| must evaluate to a sequence.  Elements from this sequence are treated
| as if they were additional positional arguments; if there are postional
| arguments X1,...,XN , and `expression' evaluates to a sequence
| Y1,...,YM, this is equivalent to a call with M+N positional arguments
| X1,...,XN,Y1,...,YM.
| 
| A consequence of this is that although the `*expression' syntax appears
| _after_ any keyword arguments, it is processed _before_ the keyword
| arguments (and the `**expression' argument, if any - see below).  So:
| 
|      >>> def f(a, b):
|      ...  print a, b
|      ...
|      >>> f(b=1, *(2,))
|      2 1
|      >>> f(a=1, *(2,))
|      Traceback (most recent call last):
|        File "<stdin>", line 1, in ?
|      TypeError: f() got multiple values for keyword argument 'a'
|      >>> f(1, *(2,))
|      1 2
`----


I see it as  analogue to the definition of functions where something
like 
     def f(*args): pass
means to collect all arguments in a tuple.  Using that syntax in a call
does the opposite (people who know Lisp may know that from macros where
you can slice a sequence with `,@').



   Karl
-- 
Please do *not* send copies of replies to me.
I read the list




More information about the Tutor mailing list