Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am studying Python3 tutorial on keyword arguments and couldn't reproduce the output due to the following code:

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)
    for kw in keywords:
        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")

-- 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
client : John Cleese
sketch : Cheese Shop Sketch 

What I got was a sorted dict:

----------------------------------------
client : John Cleese
shopkeeper : Michael Palin
sketch : Cheese Shop Sketch

So I tried without calling cheeseshop():

>>> kw = {'shopkeeper':"Michael Palin", 'client':"John Cleese", 'sketch':"Cheese Shop Sketch"}
>>> kw
{'client': 'John Cleese', 'shopkeeper': 'Michael Palin', 'sketch': 'Cheese Shop Sketch'}

It looks like in version 3.5, the keys are automatically sorted. But in version 2.7, they aren't:

>>> kw
{'shopkeeper': 'Michael Palin', 'sketch': 'Cheese Shop Sketch', 'client': 'John Cleese'}

and I have to sort it in 2.7 to agree with 3.5.

>>> for k in sorted(kw):
...     print(k + " : " + kw[k])
... 
client : John Cleese
shopkeeper : Michael Palin
sketch : Cheese Shop Sketch

So the statement in the tutorial: "Note that the order in which the keyword arguments are printed is guaranteed to match the order in which they were provided in the function call." should be only applied to version 2.7, not 3.5. Is this true?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
162 views
Welcome To Ask or Share your Answers For Others

1 Answer

Following the comments from @Ry and @abamert, I upgraded to Python 3.7, see link1 link2 and link3 for steps on how to build it from <3.7> source code. One note is that the Python default for Ubuntu 16.04 are versions 2.7 and 3.5 in /usr/bin. After the upgrade, 3.7 resides in /usr/local/bin. So we can have three versions coexistent.

As they said, do not rely on a particular version for the key order since the behavior of versions 2.7, 3.5 and 3.7 is different from each other:

  1. <2.7> order is random, but consistent/repeatable.
  2. <3.5> order is random and inconsistent.
  3. <3.7> order is preserved as entered and thus consistent.
  4. Use OrderedDict if you want it to be sorted. see below.

For example:

Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> kw = {'shopkeeper':"Michael Palin", 'client':"John Cleese", 'sketch':"Cheese Shop Sketch"}
>>> kw
{'client': 'John Cleese', 'shopkeeper': 'Michael Palin', 'sketch': 'Cheese Shop Sketch'}
>>>
>>> from collections import OrderedDict
>>> kws = OrderedDict(sorted(kw.items(), key = lambda x:x[0]))
>>> kws
OrderedDict([('client', 'John Cleese'), ('shopkeeper', 'Michael Palin'), ('sketch', 'Cheese Shop Sketch')])
>>> for i in kws:
...      print(i + " :	" + kws[i])
...
client :        John Cleese
shopkeeper :    Michael Palin
sketch :        Cheese Shop Sketch
>>>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...