Mercurial > pylearn
diff doc/v2_planning/coding_style.txt @ 1066:e1aca94f28d8
coding_style: Added suggestion from PV, and a few coding guidelines
author | Olivier Delalleau <delallea@iro> |
---|---|
date | Fri, 10 Sep 2010 09:53:50 -0400 |
parents | 64720cdca3d3 |
children | 4f287324a5ad |
line wrap: on
line diff
--- 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 +