Suggestion for Style Guide for Python Code PEP 8

Hi, I was wondering if the following programming recommendation would be added to the Style Guide for Python Code (PEP 8) page. The preferred way for checking if a key (k) exists in a dictionary (d) is "if k in d". This is faster than "if k in d.keys()" and this has superseded "d.has_key(k)" Cheers, Rahul.

On 12.05.2011 15:44, Rahul Amaram wrote:
While "k in d" is certainly the right way, this is not the sort of thing that should be added to PEP 8. There must be dozens of such little idioms and anti-idioms, and listing them all is way beyond the PEP's scope. (And has_key is gone in py3k anyway.) Georg

Thanks for the reply George and Weeble. It would nice if these kind of minor programming guidelines are also included in some page probably titled "Extended Python Guidelines" :). The reason being novice programmers in python who have worked in previous languages tend to use the same style of coding as in other languages. So, for instance, to check for the existence of a key in a dictionary, it is extremely likely that they'd either look for a has_key method or get a list of all the keys and search in it. Anyway, as you said, there might a lot of such small idioms in python, which may not make sense to cover in PEP 8 but if they are really the recommended way of doing the operation, then we probably should have them documented in one place. Regards, Rahul. On Friday 13 May 2011 01:42 AM, Georg Brandl wrote:

On Sun, May 15, 2011 at 12:17 AM, Georg Brandl <g.brandl@gmx.net> wrote:
I'd hope that simple things like "k in d" are already in every tutorial on Python that's worth anything...
In this particular case, the official docs are already quite explicit: """has_key() is deprecated in favor of key in d.""" http://docs.python.org/library/stdtypes.html#dict.has_key Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia

On 12.05.2011 15:44, Rahul Amaram wrote:
While "k in d" is certainly the right way, this is not the sort of thing that should be added to PEP 8. There must be dozens of such little idioms and anti-idioms, and listing them all is way beyond the PEP's scope. (And has_key is gone in py3k anyway.) Georg

Thanks for the reply George and Weeble. It would nice if these kind of minor programming guidelines are also included in some page probably titled "Extended Python Guidelines" :). The reason being novice programmers in python who have worked in previous languages tend to use the same style of coding as in other languages. So, for instance, to check for the existence of a key in a dictionary, it is extremely likely that they'd either look for a has_key method or get a list of all the keys and search in it. Anyway, as you said, there might a lot of such small idioms in python, which may not make sense to cover in PEP 8 but if they are really the recommended way of doing the operation, then we probably should have them documented in one place. Regards, Rahul. On Friday 13 May 2011 01:42 AM, Georg Brandl wrote:

On Sun, May 15, 2011 at 12:17 AM, Georg Brandl <g.brandl@gmx.net> wrote:
I'd hope that simple things like "k in d" are already in every tutorial on Python that's worth anything...
In this particular case, the official docs are already quite explicit: """has_key() is deprecated in favor of key in d.""" http://docs.python.org/library/stdtypes.html#dict.has_key Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia
participants (3)
-
Georg Brandl
-
Nick Coghlan
-
Rahul Amaram