Reputation: 31
I am using Python 2.7.3 and trying to understand why this script executes print statements out of order. i.e. the "-" prints AFTER the 2nd for loop.
My script:
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]
cheeseshop("Limburger", "It's very runny, sir.",
"It's really very, VERY runny, sir.",
{'shopkeeper':'Michael Palin',
'client':"John Cleese",
'sketch':"Cheese Shop Sketch"})
The output:
-- Do you have any Limburger ?
-- I'm sorry, we're all out of Limburger
It's very runny, sir.
It's really very, VERY runny, sir.
{'shopkeeper': 'Michael Palin', 'sketch': 'Cheese Shop Sketch', 'client': 'John Cleese'}
----------------------------------------
Why print "-"*40 execute BEFORE the dictionary as would be expected?
Upvotes: 3
Views: 931
Reputation: 1121864
You didn't pass in the dictionary as keywords. Use the **
syntax to do so:
cheeseshop("Limburger", "It's very runny, sir.",
"It's really very, VERY runny, sir.",
**{'shopkeeper':'Michael Palin',
'client':"John Cleese",
'sketch':"Cheese Shop Sketch"})
or don't use a dictionary at all:
cheeseshop("Limburger", "It's very runny, sir.",
"It's really very, VERY runny, sir.",
shopkeeper='Michael Palin',
client="John Cleese",
sketch="Cheese Shop Sketch")
Upvotes: 8