[Python-bugs-list] [ python-Bugs-755564 ] Tutorial: 4.7.2 Keyword Arguments **name output order wrong

SourceForge.net noreply@sourceforge.net
Tue, 17 Jun 2003 13:04:55 -0700


Bugs item #755564, was opened at 2003-06-17 00:53
Message generated for change (Comment added) made by kakkonen
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=755564&group_id=5470

Category: Documentation
Group: Not a Bug
Status: Open
Resolution: None
>Priority: 3
Submitted By: Jan Mattila (kakkonen)
Assigned to: Nobody/Anonymous (nobody)
Summary: Tutorial: 4.7.2 Keyword Arguments **name output order wrong

Initial Comment:
In the Tutorial at chapter 4.7.2 Keyword Arguments, 
there is an example about the formal parameter **name. 
I'm quoting the whole thing, for quick reference:

--- begin extract from Tutorial ---
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.keys(): print kw, ':', keywords[kw]

It could be called like this:

cheeseshop('Limburger', "It's very runny, sir.",
           "It's really very, VERY runny, sir.",
           client='John Cleese',
           shopkeeper='Michael Palin',
           sketch='Cheese Shop Sketch')

and of course it would print:

-- 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.
----------------------------------------
client : John Cleese
shopkeeper : Michael Palin
sketch : Cheese Shop Sketch
--- end extract from Tutorial ---

What it actually prints on my LFS 3.3 (linuxfromscratch)
based distribution with Python 2.1.3 is

--- begin print out of cheeseshop ---
-- 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.
----------------------------------------
client : John Cleese
sketch : Cheese Shop Sketch
shopkeeper : Michael Palin
--- end print out of cheeseshop ---

The problem is that the keywords[kw] list order is
garbled. I tried stripping this function of the *arguments
formal parameter and I got the same result. I tried
different amounts of **keywords in different
aphabetical orders. The resulting order does not 
seem to be random and does not seem to be aplhabetical
nor related to the length of the **keywords or anything
I could figure out.

It's probably not a typo (on my part), because I copied 
the code straight from the Tutorial page. 

Now, this is not really a bug in my opinion, so I'm not 
expecting a fix, but I was just trying to read through
the Tutorial and try all the example code and got 
stuck trying to figure how you could tell the print order 
of a formal parameter **name -type's name[kw] list.

I was thinking, that maybe if someone of class wizard 
or higher would care to indulge me in the reason for 
this behaviour I could become enlightened on another 
aspect of Python. I'm not expecting swift replies, since
this is a zero-priority glitch, but someone, somewhere,
someday, maybe? No? Okay. Just a thought...

So much for trying to make this short.

I managed to reproduce the problem on Python 1.5.2
on Red Hat Linux 7.3.2 on linux-i386

----------------------------------------------------------------------

>Comment By: Jan Mattila (kakkonen)
Date: 2003-06-17 23:04

Message:
Logged In: YES 
user_id=802538

Thanks  Christopher,

Your reply solved the mystery, but I was unable to find the 
text you mentioned in the Tutorial at:

http://www.python.org/doc/2.1.3/tut/tut.html

I tried searching the whole thing, just in case, and the
closest 
I found was this:

"The keys() method of a dictionary object returns a list of all 
the keys used in the dictionary, in random order (if you want 
it sorted, just apply the sort() method to the list of keys)."

But it was in chapter 5.4 Dictionaries, which is quite a way
from the mystery at Chapter 4.7.2. 

I mean the text you quoted? would solve the mystery instantly
and even shed light on how to fix it. That is, if it was in the
tutorial.

My question is: Can you or someone put it there?

Additionally it would be nice to either exclude the sorted
list from the Tutorial or add a snippet of code for reproducing
the list exactly. 

I think this could do it (although I'm sure someone knows
a better way, and that should go into the tutorial):

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
        info = keywords.items()
        info.sort()
        for value in info:
                print value[0], ':', value[1]


-Jan

(forgot to sign my last message...)

----------------------------------------------------------------------

Comment By: Christopher Blunck (blunck2)
Date: 2003-06-17 06:45

Message:
Logged In: YES 
user_id=531881

Hi Jan-

The tutorial points out:

Note that the sort() method of the list of keyword argument
names is called before printing the contents of the keywords
dictionary; if this is not done, the order in which the
arguments are
printed is undefined.

Not sure if a patch is necessary :)


-c

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=755564&group_id=5470