Skip to content

Commit f84020e

Browse files
Google Python teamgpshead
authored andcommitted
Project import generated by Copybara.
PiperOrigin-RevId: 332244028
1 parent 32916a9 commit f84020e

File tree

1 file changed

+16
-31
lines changed

1 file changed

+16
-31
lines changed

pyguide.md

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -463,10 +463,6 @@ library calls.
463463

464464
Exceptions must follow certain conditions:
465465

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-
470466
- Make use of built-in exception classes when it makes sense. For example,
471467
raise a `ValueError` to indicate a programming mistake like a violated
472468
precondition (such as if you were passed a negative number but required a
@@ -548,16 +544,6 @@ Exceptions must follow certain conditions:
548544
raised in the `try` block. This is often useful for cleanup, i.e., closing a
549545
file.
550546

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-
561547
<a id="s2.5-global-variables"></a>
562548
<a id="25-global-variables"></a>
563549

@@ -1376,8 +1362,8 @@ Okay to use.
13761362
<a id="decorators"></a>
13771363
### 2.17 Function and Method Decorators
13781364
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`.
13811367
13821368
<a id="s2.17.1-definition"></a>
13831369
<a id="2171-definition"></a>
@@ -1447,10 +1433,10 @@ guaranteed to succeed in all cases.
14471433
Decorators are a special case of "top level code" - see [main](#s3.17-main) for
14481434
more discussion.
14491435
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
14511437
defined in an existing library. Write a module level function instead.
14521438
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
14541440
routine that modifies necessary global state such as a process-wide cache.
14551441
14561442
<a id="s2.18-threading"></a>
@@ -2188,7 +2174,7 @@ aptly described using a one-line docstring.
21882174
def fetch_smalltable_rows(table_handle: smalltable.Table,
21892175
keys: Sequence[Union[bytes, str]],
21902176
require_all_keys: bool = False,
2191-
) -> Mapping[bytes, Tuple[str]]:
2177+
) -> Mapping[bytes, Tuple[str]]:
21922178
"""Fetches rows from a Smalltable.
21932179
21942180
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:
22252211
def fetch_smalltable_rows(table_handle: smalltable.Table,
22262212
keys: Sequence[Union[bytes, str]],
22272213
require_all_keys: bool = False,
2228-
) -> Mapping[bytes, Tuple[str]]:
2214+
) -> Mapping[bytes, Tuple[str]]:
22292215
"""Fetches rows from a Smalltable.
22302216
22312217
Retrieves rows pertaining to the given keys from the Table instance
@@ -2649,8 +2635,8 @@ grouped from most generic to least generic:
26492635
26502636
26512637
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.
26542640
26552641
```python
26562642
import collections
@@ -2664,6 +2650,7 @@ import cryptography
26642650
import tensorflow as tf
26652651
26662652
from book.genres import scifi
2653+
from myproject.backend import huxley
26672654
from myproject.backend.hgwells import time_machine
26682655
from myproject.backend.state_machine import main_loop
26692656
from otherproject.ai import body
@@ -2756,7 +2743,7 @@ Always use a `.py` filename extension. Never use dashes.
27562743
27572744
- single character names, except for specifically allowed cases:
27582745
2759-
- counters or iterators (e.g. `i`, `j`, `k`, `v`, et al)
2746+
- counters or iterators (e.g. `i`, `j`, `k`, `v`, et al.)
27602747
- `e` as an exception identifier in `try/except` statements.
27612748
- `f` as a file handle in `with` statements
27622749
@@ -2904,13 +2891,11 @@ containing `exec "$0.py" "$@"`.
29042891
<a id="main"></a>
29052892
### 3.17 Main
29062893
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.
29142899
29152900
When using [absl](https://github.com/abseil/abseil-py), use `app.run`:
29162901
@@ -3142,7 +3127,7 @@ has to be declared! You can use `Union`, but if there is only one other type,
31423127
use `Optional`.
31433128
31443129
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`,
31463131
but that is no longer the preferred behavior.
31473132
31483133
```python

0 commit comments

Comments
 (0)