2

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.

5
  • It's not clear what you are asking. Please clarify. Commented Jul 16, 2013 at 13:19
  • 3
    keywords is a dictionary. keywords[kw] is a value of the keyword dictionary and kw is a key from that dictionary as well. So when you "print keywords", you get the full representation of the dictionary, not just names. Commented Jul 16, 2013 at 13:19
  • you are a bit confused about dictionaries in python? Commented Jul 16, 2013 at 13:21
  • njzk2 - yes, I think so. I'm missing something. dilbert - so, keywords[kw] is actually referencing a value, and not a key? ie: keywords[kw] is synonymous with keywords.values()? Commented Jul 16, 2013 at 13:24
  • keywords[kw] is synonymous with a SINGLE element of the list that is keywords.values(). Commented Jul 16, 2013 at 13:47

3 Answers 3

2

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'

Sign up to request clarification or add additional context in comments.

2 Comments

Ahhhh... okay. This makes sense. This is the part I was missing. Thank you. :) Sorry the question was a little garbled... I wasn't sure how to phrase it without being overly verbose.
No problem. Hope this clarifies!
0

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

1 Comment

No problem! Dictionaries are really useful, but be aware when to use them and when arrays are better (performance, size, etc)
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.