Issue755564
This issue tracker has been migrated to GitHub,
and is currently read-only.
For more information,
see the GitHub FAQs in the Python's Developer Guide.
Created on 2003-06-16 21:53 by kakkonen, last changed 2022-04-10 16:09 by admin. This issue is now closed.
Files | ||||
---|---|---|---|---|
File name | Uploaded | Description | Edit | |
4.7.2 proposed text.html | kakkonen, 2003-06-17 22:10 | proposed replacement for Python 2.1.3 Tutorial chapter 4.7.2 |
Messages (5) | |||
---|---|---|---|
msg16436 - (view) | Author: Jan Mattila (kakkonen) | Date: 2003-06-16 21:53 | |
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 |
|||
msg16437 - (view) | Author: Christopher Blunck (blunck2) | Date: 2003-06-17 03:45 | |
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 |
|||
msg16438 - (view) | Author: Jan Mattila (kakkonen) | Date: 2003-06-17 20:04 | |
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...) |
|||
msg16439 - (view) | Author: Jan Mattila (kakkonen) | Date: 2003-06-17 22:10 | |
Logged In: YES user_id=802538 Right, Can you force the submitter's comments to fall in line with the rest of the comments, instead of getting pushed to the front of the que? This order breaks the chain of comments and, is IMHO more difficult to read. Be that as it may. I read a couple of these bug report thingies and felt that I should propose some text in html format if I wanted anything to go anywhere, without burdening people with genuinely important things to do. I'm attaching a file with the bit of code from the last comment and some explanatory text in html format (please excuse the language). The text is proposed to replace chapter 4.7.2 in the Python 2.1.3 Tutorial at: http://www.python.org/doc/2.1.3/tut/node6.html#SECTION006720000000000000000 N.B. There are two comments in the html, so if none are allowed in the tutorial html, you know what to do with them. -Jan P.S. I realise that putting all of this under the heading "Keyword arguments" is stretching it beyond breaking point. Thus maybe the quickest way would be to just add the text from chapter 5.4 to chapter 4.7.2, something like this: "Note that the keys() method of a dictionary object returns a list of all the keys used in the dictionary, in random order." |
|||
msg16440 - (view) | Author: Raymond Hettinger (rhettinger) * ![]() |
Date: 2003-07-10 16:40 | |
Logged In: YES user_id=80475 This was already fixed in 2.2.3 and 2.3b2 by adding keys.sort () to the example. |
History | |||
---|---|---|---|
Date | User | Action | Args |
2022-04-10 16:09:15 | admin | set | github: 38660 |
2003-06-16 21:53:09 | kakkonen | create |