Re: The ** operator ambiguous?

2007-07-17 Thread Duncan Booth
Paul Boddie [EMAIL PROTECTED] wrote:

 However, this ambiguous usage of * and ** is one thing I don't recall
 appearing on any of the Python warts lists

It is true that the same punctuation character is used in more than one
context, but that is also true for many other punctuation characters.

There is no ambiguity.
-- 
http://mail.python.org/mailman/listinfo/python-list


The ** operator ambiguous?

2007-07-16 Thread Robert Dailey
I noticed that the ** operator is used as the power operator, however
I've seen it used when passing variables into a function. For example,
I was researching a way to combine dictionaries. I found that if you
do this:

a = {t1:a, t2:b}
b = {t3:c}
dict( a, **b )


This combines the two dictionaries. However, I have no idea what the
** operator is here. I know that when you specify ** as a parameter in
a function definition, it represents a dictionary of parameters passed
in. However, in this example it is NOT being used in a function
definition. It is being used when passing variables into a function.
Can someone explain what this means? I looked in the documentation but
I couldn't find anything.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: The ** operator ambiguous?

2007-07-16 Thread Diez B. Roggisch
Robert Dailey schrieb:
 I noticed that the ** operator is used as the power operator, however
 I've seen it used when passing variables into a function. For example,
 I was researching a way to combine dictionaries. I found that if you
 do this:
 
 a = {t1:a, t2:b}
 b = {t3:c}
 dict( a, **b )
 
 
 This combines the two dictionaries. However, I have no idea what the
 ** operator is here. I know that when you specify ** as a parameter in
 a function definition, it represents a dictionary of parameters passed
 in. However, in this example it is NOT being used in a function
 definition. It is being used when passing variables into a function.
 Can someone explain what this means? I looked in the documentation but
 I couldn't find anything.

It's the opposite to the function definition **-operator:

def foo(a=0, b=1):
 print a,b


foo(**{a:100, b:200})

- 100 200

Diez
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: The ** operator ambiguous?

2007-07-16 Thread Duncan Booth
Robert Dailey [EMAIL PROTECTED] wrote:

 However, I have no idea what the
 ** operator is here. I know that when you specify ** as a parameter in
 a function definition, it represents a dictionary of parameters passed
 in. However, in this example it is NOT being used in a function
 definition. It is being used when passing variables into a function.
 Can someone explain what this means? I looked in the documentation but
 I couldn't find anything.
 

It is in the documentation. Look in the reference manual section 5.3.4 
Calls (http://docs.python.org/ref/calls.html):

 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
 
 It is unusual for both keyword arguments and the *expression syntax
 to be used in the same call, so in practice this confusion does not
 arise. 
 
 If the syntax **expression appears in the function call,
 expression must evaluate to a (subclass of) dictionary, the contents
 of which are treated as additional keyword arguments. In the case of a
 keyword appearing in both expression and as an explicit keyword
 argument, a TypeError exception is raised. 

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: The ** operator ambiguous?

2007-07-16 Thread Gabriel Genellina
En Mon, 16 Jul 2007 14:40:14 -0300, Robert Dailey [EMAIL PROTECTED]  
escribió:

 I noticed that the ** operator is used as the power operator, however
 I've seen it used when passing variables into a function. For example,
 I was researching a way to combine dictionaries. I found that if you
 do this:

 a = {t1:a, t2:b}
 b = {t3:c}
 dict( a, **b )


 This combines the two dictionaries. However, I have no idea what the
 ** operator is here. I know that when you specify ** as a parameter in
 a function definition, it represents a dictionary of parameters passed
 in. However, in this example it is NOT being used in a function
 definition. It is being used when passing variables into a function.
 Can someone explain what this means? I looked in the documentation but
 I couldn't find anything.

See this section in the tutorial  
http://docs.python.org/tut/node6.html#SECTION00670.
If you want the dirty details: http://docs.python.org/ref/calls.html

-- 
Gabriel Genellina

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: The ** operator ambiguous?

2007-07-16 Thread Klaas
On Jul 16, 10:40 am, Robert Dailey [EMAIL PROTECTED] wrote:
 I noticed that the ** operator is used as the power operator, however
 I've seen it used when passing variables into a function. For example,
 I was researching a way to combine dictionaries. I found that if you
 do this:

 a = {t1:a, t2:b}
 b = {t3:c}
 dict( a, **b )

 This combines the two dictionaries.

Use dict.update to combine dictionaries.

-Mike

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: The ** operator ambiguous?

2007-07-16 Thread Paul Boddie
Robert Dailey wrote:
 I noticed that the ** operator is used as the power operator, however
 I've seen it used when passing variables into a function.

Others have already pointed out the relevant documentation. However,
this ambiguous usage of * and ** is one thing I don't recall appearing
on any of the Python warts lists - not that I spend too much time
following such matters. I imagine that * was initially chosen in order
to be similar to the way one may handle collections of values in C
function signatures (ie. using pointers), and that ** was merely a
convenient next step as opposed to being analogous to the pointer to
pointer notation from C. The same symbols are used in different ways
in C and C++, of course.

It's interesting to see a fresh interpretation of the notation,
though.

Paul

-- 
http://mail.python.org/mailman/listinfo/python-list