I'm going through the Python 2.7 tutorial, and I was looking at the output of the following statement:
def cheeseshop(kind, *arguments, **keywords):
print "-- Do you have any", kind, "?"
print "-- I'm sorry, we're all out of", kind
for arg in arguments:
print arg
print "-" * 40
keys = sorted(keywords.keys())
for kw in keys:
print kw, ":", keywords[kw]
So, if I call the program as such:
cheeseshop("Cheddar", "No.", "Seriously?",
Shopkeeper="Michael Palin",
Client="John Cleese")
It outputs:
Do you have any Cheddar?
I'm sorry, we're all out of Cheddar
No.
Seriously?
--------------------------------------
Client: John Cleese
Shopkeeper: Michael Palin
This is correct.
If I change that print statement to print keywords
, I get the following representation:
{'Shopkeeper': 'Ryan Lambert', 'Client': 'John Cleese'}
I'm a bit confused on how printing keywords[kw]
just comes back with a name, and keywords
does not.
In Python, you can pass optional keyword parameters by putting a **
in front of the function parameter's list.
So the keywords
variable is actually a dictionary type. Thus, if you do:
print keywords
you get back (reformatted to make the mapping more obvious)
{
'Shopkeeper': 'Ryan Lambert',
'Client': 'John Cleese'
}
which is a dictionary. And if you do:
print keywords[kw]
you get back the value of the dictionary associated with the key kw
. So if kw
was 'Shopkeeper'
, then keywords[kw]
becomes 'Ryan Lambert'
, and if kw
was 'Client'
, then keywords[kw]
becomes 'John Cleese'
keywords is stored as a dictionary. ( See this for more)
If you print the dictionary itself it is going to output the complete set of pairs it contains (key,value).
On your program:
- keys are: 'Shopkeeper' and 'Client'
- values are respectively: 'Ryan Lambert' and 'John Cleese'
One way to access the values is to "search" for it with its key: dict[key]
So when you wrote: "keywords[kw]" you are actually passing a key and python is going to give you the corresponding value.
You can think it as similar as accessing an array value:
a = ['a', 'b', 'c']
if you:
print a #output: ['a', 'b', 'c']
print a[0] # outputs: 'a'
just unlike arrays the data is not stored "neatly" together in memory, but using hashing
Hope it helped, Cheers
When you call the function with
cheeseshop("Cheddar", "No.", "Seriously?",
Shopkeeper="Michael Palin", Client="John Cleese")
the keywords
parameter takes on the value {'Shopkeeper': 'Ryan Lambert', 'Client': 'John Cleese'}
, i.e., it's a dictionary.
This is equivalent to (and much easier to read than) calling the function as
cheeseshop("Cheddar", *["No.", "Seriously?"],
**{"Shopkeeper":"Michael Palin", "Client":"John Cleese"})
That is, the values in the first function call are automatically wrapped inside the *arguments
and **keywords
parameters -- that's what those *
and **
are for.
Now, when you do this:
keys = sorted(keywords.keys())
for kw in keys:
print kw, ":", keywords[kw]
keyword.keys()
is ['Shopkeeper', 'Client']
, i.e. the "keys" in the dictionary. Next, you sort those keys and for each key, you print the respective entry in the dictionary, e.g., "John Cleese" for "Client".