# HG changeset patch # User Olivier Delalleau # Date 1284126830 14400 # Node ID e1aca94f28d83fdca7af038d29bd0ec413adead1 # Parent 64720cdca3d3d43f6282d508562d9c4031c5df0e coding_style: Added suggestion from PV, and a few coding guidelines diff -r 64720cdca3d3 -r e1aca94f28d8 doc/v2_planning/coding_style.txt --- a/doc/v2_planning/coding_style.txt Thu Sep 09 13:21:20 2010 -0400 +++ b/doc/v2_planning/coding_style.txt Fri Sep 10 09:53:50 2010 -0400 @@ -152,3 +152,38 @@ To enforce good coding style automatically. Task: Look for existing options. (FB) +Suggestion by PV +---------------- + +Have a sample code that showcases everything one should comply to. + +Some coding guidlines (work-in-progress from OD) +------------------------------------------------ + + * Avoid using lists if all you care about is iterating on something. Using + lists: + - uses more memory (and possibly more CPU if the code may break out of + the iteration) + - can lead to ugly code when converted to Python 3 with 2to3 + - can have a different behavior if evaluating elements in the list has + side effects (if you want these side effects, make it explicit by + assigning the list to some variable before iterating on it) + + Iterative version List version + my_dict.iterkeys() my_dict.keys() + my_dict.itervalues() my_dict.values() + my_dict.iteritems() my_dict.items() + itertools.imap map + itertools.ifilter filter + itertools.izip zip + xrange range + + * Use `in` on container objects instead of using class-specific methods. + It is easier to read and may allow you to use your code with different + container types. + + Yes No + --- -- + key in my_dict my_dict.has_key(key) + sub_string in my_string my_string.find(sub_string) >= 0 +