@@ -463,10 +463,6 @@ library calls.
463
463
464
464
Exceptions must follow certain conditions:
465
465
466
- - Raise exceptions like this: ` raise MyError('Error message') ` or `raise
467
- MyError()` . Do not use the two-argument form ( ` raise MyError, 'Error
468
- message'`).
469
-
470
466
- Make use of built-in exception classes when it makes sense. For example,
471
467
raise a ` ValueError ` to indicate a programming mistake like a violated
472
468
precondition (such as if you were passed a negative number but required a
@@ -548,16 +544,6 @@ Exceptions must follow certain conditions:
548
544
raised in the `try ` block. This is often useful for cleanup, i.e., closing a
549
545
file .
550
546
551
- - When capturing an exception, use `as ` rather than a comma. For example:
552
-
553
-
554
- ```python
555
- try :
556
- raise Error()
557
- except Error as error:
558
- pass
559
- ```
560
-
561
547
< a id =" s2.5-global-variables" >< / a>
562
548
< a id =" 25-global-variables" >< / a>
563
549
@@ -1376,8 +1362,8 @@ Okay to use.
1376
1362
< a id = " decorators" >< / a>
1377
1363
# ## 2.17 Function and Method Decorators
1378
1364
1379
- Use decorators judiciously when there is a clear advantage. Avoid
1380
- ` @ staticmethod ` and limit use of `@ classmethod ` .
1365
+ Use decorators judiciously when there is a clear advantage. Avoid ` staticmethod `
1366
+ and limit use of `classmethod ` .
1381
1367
1382
1368
< a id = " s2.17.1-definition" >< / a>
1383
1369
< a id = " 2171-definition" >< / a>
@@ -1447,10 +1433,10 @@ guaranteed to succeed in all cases.
1447
1433
Decorators are a special case of " top level code" - see [main](# s3.17-main) for
1448
1434
more discussion.
1449
1435
1450
- Never use `@ staticmethod ` unless forced to in order to integrate with an API
1436
+ Never use `staticmethod ` unless forced to in order to integrate with an API
1451
1437
defined in an existing library. Write a module level function instead.
1452
1438
1453
- Use `@ classmethod ` only when writing a named constructor or a class - specific
1439
+ Use `classmethod ` only when writing a named constructor or a class - specific
1454
1440
routine that modifies necessary global state such as a process- wide cache.
1455
1441
1456
1442
< a id = " s2.18-threading" >< / a>
@@ -2188,7 +2174,7 @@ aptly described using a one-line docstring.
2188
2174
def fetch_smalltable_rows(table_handle: smalltable.Table,
2189
2175
keys: Sequence[Union[bytes, str]],
2190
2176
require_all_keys: bool = False,
2191
- ) -> Mapping[bytes, Tuple[str]]:
2177
+ ) -> Mapping[bytes, Tuple[str]]:
2192
2178
""" Fetches rows from a Smalltable.
2193
2179
2194
2180
Retrieves rows pertaining to the given keys from the Table instance
@@ -2225,7 +2211,7 @@ Similarly, this variation on `Args:` with a line break is also allowed:
2225
2211
def fetch_smalltable_rows(table_handle: smalltable.Table,
2226
2212
keys: Sequence[Union[bytes, str]],
2227
2213
require_all_keys: bool = False,
2228
- ) -> Mapping[bytes, Tuple[str]]:
2214
+ ) -> Mapping[bytes, Tuple[str]]:
2229
2215
""" Fetches rows from a Smalltable.
2230
2216
2231
2217
Retrieves rows pertaining to the given keys from the Table instance
@@ -2649,8 +2635,8 @@ grouped from most generic to least generic:
2649
2635
2650
2636
2651
2637
Within each grouping, imports should be sorted lexicographically, ignoring case,
2652
- according to each module' s full package path. Code may optionally place a blank
2653
- line between import sections.
2638
+ according to each module' s full package path (the `path` in `from path import
2639
+ ... `). Code may optionally place a blank line between import sections.
2654
2640
2655
2641
```python
2656
2642
import collections
@@ -2664,6 +2650,7 @@ import cryptography
2664
2650
import tensorflow as tf
2665
2651
2666
2652
from book.genres import scifi
2653
+ from myproject.backend import huxley
2667
2654
from myproject.backend.hgwells import time_machine
2668
2655
from myproject.backend.state_machine import main_loop
2669
2656
from otherproject.ai import body
@@ -2756,7 +2743,7 @@ Always use a `.py` filename extension. Never use dashes.
2756
2743
2757
2744
- single character names, except for specifically allowed cases:
2758
2745
2759
- - counters or iterators (e.g. `i` , `j` , `k` , `v` , et al)
2746
+ - counters or iterators (e.g. `i` , `j` , `k` , `v` , et al. )
2760
2747
- `e` as an exception identifier in `try / except ` statements.
2761
2748
- `f` as a file handle in `with ` statements
2762
2749
@@ -2904,13 +2891,11 @@ containing `exec "$0.py" "$@"`.
2904
2891
< a id =" main" >< / a>
2905
2892
# ## 3.17 Main
2906
2893
2907
- Even a file meant to be used as an executable should be importable and a mere
2908
- import should not have the side effect of executing the program' s main
2909
- functionality. The main functionality should be in a `main()` function.
2910
-
2911
- In Python, `pydoc` as well as unit tests require modules to be importable. Your
2912
- code should always check `if __name__ == ' __main__' ` before executing your main
2913
- program so that the main program is not executed when the module is imported.
2894
+ In Python, `pydoc` as well as unit tests require modules to be importable. If a
2895
+ file is meant to be used as an executable, its main functionality should be in a
2896
+ `main()` function, and your code should always check `if __name__ == ' __main__' `
2897
+ before executing your main program, so that it is not executed when the module
2898
+ is imported.
2914
2899
2915
2900
When using [absl](https:// github.com/ abseil/ abseil- py), use `app.run` :
2916
2901
@@ -3142,7 +3127,7 @@ has to be declared! You can use `Union`, but if there is only one other type,
3142
3127
use `Optional` .
3143
3128
3144
3129
Use explicit `Optional` instead of implicit `Optional` . Earlier versions of PEP
3145
- 484 allowed `a: Text = None ` to be interpretted as `a: Optional[Text] = None ` ,
3130
+ 484 allowed `a: Text = None ` to be interpreted as `a: Optional[Text] = None ` ,
3146
3131
but that is no longer the preferred behavior.
3147
3132
3148
3133
```python
0 commit comments