[Python-ideas] Suggestion for Style Guide for Python Code PEP 8

Rahul Amaram amaramrahul at users.sourceforge.net
Sat May 14 06:50:33 CEST 2011


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 12.05.2011 15:44, Rahul Amaram wrote:
>    
>> 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)"
>>      
> 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
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>    



More information about the Python-ideas mailing list