The ** operator ambiguous?

R

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.
 
D

Diez B. Roggisch

Robert said:
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
 
D

Duncan Booth

Robert Dailey said:
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):
 
G

Gabriel Genellina

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#SECTION006700000000000000000>.
If you want the dirty details: <http://docs.python.org/ref/calls.html>
 
K

Klaas

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
 
P

Paul Boddie

Robert said:
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
 
D

Duncan Booth

Paul Boddie said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top