comparison doc/v2_planning/API_coding_style.txt @ 1184:5783948b3f8c

API_coding_style: Moved the point about how to raise exception since it is found in official documents
author Olivier Delalleau <delallea@iro>
date Fri, 17 Sep 2010 16:58:16 -0400
parents bc1b445e22fa
children d94d1675c7e6
comparison
equal deleted inserted replaced
1183:bc1b445e22fa 1184:5783948b3f8c
218 ... 218 ...
219 # Bad. 219 # Bad.
220 def f(array=[]): # Dangerous if `array` is modified down the road. 220 def f(array=[]): # Dangerous if `array` is modified down the road.
221 ... 221 ...
222 222
223 * Always raise an exception with ``raise MyException(args)`` where ``MyException``
224 inherits from ``Exception``. This is required for compatibility across
225 all versions of Python.
226
227 .. code-block:: python
228
229 # Good.
230 raise NotImplementedError('The Pylearn team is too lazy.')
231 # Bad.
232 raise NotImplementedError, 'The Pylearn team is too lazy.'
233 raise 'The Pylearn team is too lazy to implement this.'
234
223 * Use a leading underscore '_' in names of internal attributes / methods, 235 * Use a leading underscore '_' in names of internal attributes / methods,
224 but avoid the double underscore '__' unless you know what you are 236 but avoid the double underscore '__' unless you know what you are
225 doing. 237 doing.
226 238
227 239
251 n_samples_per_split = n_samples // n_splits 263 n_samples_per_split = n_samples // n_splits
252 mean_x = sum(x) / float(len(x)) 264 mean_x = sum(x) / float(len(x))
253 # Bad. 265 # Bad.
254 n_samples_per_split = n_samples / n_splits 266 n_samples_per_split = n_samples / n_splits
255 mean_x = sum(x) / len(x) 267 mean_x = sum(x) / len(x)
256
257 * Always raise an exception with ``raise MyException(args)`` where ``MyException``
258 inherits from ``Exception``. This is required for compatibility across
259 all versions of Python.
260
261 .. code-block:: python
262
263 # Good.
264 raise NotImplementedError('The Pylearn team is too lazy.')
265 # Bad.
266 raise NotImplementedError, 'The Pylearn team is too lazy.'
267 raise 'The Pylearn team is too lazy to implement this.'
268 268
269 * Use either ``try ... except`` or ``try ... finally``, but do not mix 269 * Use either ``try ... except`` or ``try ... finally``, but do not mix
270 ``except`` with ``finally`` (which is not supported in Python 2.4). 270 ``except`` with ``finally`` (which is not supported in Python 2.4).
271 You can however embed one into the other to mimic the ``try ... except ... 271 You can however embed one into the other to mimic the ``try ... except ...
272 finally`` behavior. 272 finally`` behavior.