@@ -68,7 +68,7 @@ Some other good reasons to ignore a particular guideline:
68
68
Python that don't support the feature recommended by the style guide.
69
69
70
70
71
- Code lay -out
71
+ Code Lay -out
72
72
============
73
73
74
74
Indentation
@@ -179,7 +179,6 @@ starts the multiline construct, as in::
179
179
'd', 'e', 'f',
180
180
)
181
181
182
-
183
182
Tabs or Spaces?
184
183
---------------
185
184
@@ -198,7 +197,6 @@ the ``-t`` option, it issues warnings about code that illegally mixes
198
197
tabs and spaces. When using ``-tt`` these warnings become errors.
199
198
These options are highly recommended!
200
199
201
-
202
200
Maximum Line Length
203
201
-------------------
204
202
@@ -249,7 +247,6 @@ Another such case is with ``assert`` statements.
249
247
250
248
Make sure to indent the continued line appropriately.
251
249
252
-
253
250
Should a Line Break Before or After a Binary Operator?
254
251
------------------------------------------------------
255
252
@@ -287,7 +284,6 @@ In Python code, it is permissible to break before or after a binary
287
284
operator, as long as the convention is consistent locally. For new
288
285
code Knuth's style is suggested.
289
286
290
-
291
287
Blank Lines
292
288
-----------
293
289
@@ -309,7 +305,6 @@ you may use them to separate pages of related sections of your file.
309
305
Note, some editors and web-based code viewers may not recognize
310
306
control-L as a form feed and will show another glyph in its place.
311
307
312
-
313
308
Source File Encoding
314
309
--------------------
315
310
@@ -339,11 +334,10 @@ a transliteration of their names in this character set.
339
334
Open source projects with a global audience are encouraged to adopt a
340
335
similar policy.
341
336
342
-
343
337
Imports
344
338
-------
345
339
346
- - Imports should usually be on separate lines, e.g. ::
340
+ - Imports should usually be on separate lines::
347
341
348
342
Yes: import os
349
343
import sys
@@ -370,16 +364,16 @@ Imports
370
364
messages) if the import system is incorrectly configured (such as
371
365
when a directory inside a package ends up on ``sys.path``)::
372
366
373
- import mypkg.sibling
374
- from mypkg import sibling
375
- from mypkg.sibling import example
367
+ import mypkg.sibling
368
+ from mypkg import sibling
369
+ from mypkg.sibling import example
376
370
377
371
However, explicit relative imports are an acceptable alternative to
378
372
absolute imports, especially when dealing with complex package layouts
379
373
where using absolute imports would be unnecessarily verbose::
380
374
381
- from . import sibling
382
- from .sibling import example
375
+ from . import sibling
376
+ from .sibling import example
383
377
384
378
Standard library code should avoid complex package layouts and always
385
379
use absolute imports.
@@ -393,7 +387,7 @@ Imports
393
387
from myclass import MyClass
394
388
from foo.bar.yourclass import YourClass
395
389
396
- If this spelling causes local name clashes, then spell them ::
390
+ If this spelling causes local name clashes, then spell them explicitly ::
397
391
398
392
import myclass
399
393
import foo.bar.yourclass
@@ -412,18 +406,15 @@ Imports
412
406
When republishing names this way, the guidelines below regarding
413
407
public and internal interfaces still apply.
414
408
415
-
416
- Module level dunder names
409
+ Module Level Dunder Names
417
410
-------------------------
418
411
419
412
Module level "dunders" (i.e. names with two leading and two trailing
420
413
underscores) such as ``__all__``, ``__author__``, ``__version__``,
421
414
etc. should be placed after the module docstring but before any import
422
415
statements *except* ``from __future__`` imports. Python mandates that
423
416
future-imports must appear in the module before any other code except
424
- docstrings.
425
-
426
- For example::
417
+ docstrings::
427
418
428
419
"""This is the example module.
429
420
@@ -524,7 +515,6 @@ Avoid extraneous whitespace in the following situations:
524
515
y = 2
525
516
long_variable = 3
526
517
527
-
528
518
Other Recommendations
529
519
---------------------
530
520
@@ -642,6 +632,7 @@ Other Recommendations
642
632
643
633
if foo == 'blah': one(); two(); three()
644
634
635
+
645
636
When to Use Trailing Commas
646
637
===========================
647
638
@@ -748,7 +739,7 @@ Conventions for writing good documentation strings
748
739
749
740
- PEP 257 describes good docstring conventions. Note that most
750
741
importantly, the ``"""`` that ends a multiline docstring should be
751
- on a line by itself, e.g. ::
742
+ on a line by itself::
752
743
753
744
"""Return a foobang
754
745
@@ -888,12 +879,12 @@ Type Variable Names
888
879
Names of type variables introduced in PEP 484 should normally use CapWords
889
880
preferring short names: ``T``, ``AnyStr``, ``Num``. It is recommended to add
890
881
suffixes ``_co`` or ``_contra`` to the variables used to declare covariant
891
- or contravariant behavior correspondingly. Examples ::
882
+ or contravariant behavior correspondingly::
892
883
893
- from typing import TypeVar
884
+ from typing import TypeVar
894
885
895
- VT_co = TypeVar('VT_co', covariant=True)
896
- KT_contra = TypeVar('KT_contra', contravariant=True)
886
+ VT_co = TypeVar('VT_co', covariant=True)
887
+ KT_contra = TypeVar('KT_contra', contravariant=True)
897
888
898
889
Exception Names
899
890
~~~~~~~~~~~~~~~
@@ -966,7 +957,7 @@ Constants are usually defined on a module level and written in all
966
957
capital letters with underscores separating words. Examples include
967
958
``MAX_OVERFLOW`` and ``TOTAL``.
968
959
969
- Designing for inheritance
960
+ Designing for Inheritance
970
961
~~~~~~~~~~~~~~~~~~~~~~~~~
971
962
972
963
Always decide whether a class's methods and instance variables
@@ -1041,7 +1032,6 @@ With this in mind, here are the Pythonic guidelines:
1041
1032
need to avoid accidental name clashes with potential use by
1042
1033
advanced callers.
1043
1034
1044
-
1045
1035
Public and Internal Interfaces
1046
1036
------------------------------
1047
1037
@@ -1180,9 +1170,7 @@ Programming Recommendations
1180
1170
continuation characters thanks to the containing parentheses.
1181
1171
1182
1172
- When catching exceptions, mention specific exceptions whenever
1183
- possible instead of using a bare ``except:`` clause.
1184
-
1185
- For example, use::
1173
+ possible instead of using a bare ``except:`` clause::
1186
1174
1187
1175
try:
1188
1176
import platform_specific_module
@@ -1250,17 +1238,16 @@ Programming Recommendations
1250
1238
1251
1239
- Context managers should be invoked through separate functions or methods
1252
1240
whenever they do something other than acquire and release resources.
1253
- For example:
1254
1241
1255
1242
Yes::
1256
1243
1257
- with conn.begin_transaction():
1258
- do_stuff_in_transaction(conn)
1244
+ with conn.begin_transaction():
1245
+ do_stuff_in_transaction(conn)
1259
1246
1260
1247
No::
1261
1248
1262
- with conn:
1263
- do_stuff_in_transaction(conn)
1249
+ with conn:
1250
+ do_stuff_in_transaction(conn)
1264
1251
1265
1252
The latter example doesn't provide any information to indicate that
1266
1253
the ``__enter__`` and ``__exit__`` methods are doing something other
@@ -1307,8 +1294,7 @@ Programming Recommendations
1307
1294
- Use ``''.startswith()`` and ``''.endswith()`` instead of string
1308
1295
slicing to check for prefixes or suffixes.
1309
1296
1310
- startswith() and endswith() are cleaner and less error prone. For
1311
- example::
1297
+ startswith() and endswith() are cleaner and less error prone::
1312
1298
1313
1299
Yes: if foo.startswith('bar'):
1314
1300
No: if foo[:3] == 'bar':
@@ -1328,16 +1314,16 @@ Programming Recommendations
1328
1314
1329
1315
Note that in Python 3, ``unicode`` and ``basestring`` no longer exist
1330
1316
(there is only ``str``) and a bytes object is no longer a kind of
1331
- string (it is a sequence of integers instead)
1317
+ string (it is a sequence of integers instead).
1332
1318
1333
1319
- For sequences, (strings, lists, tuples), use the fact that empty
1334
1320
sequences are false. ::
1335
1321
1336
1322
Yes: if not seq:
1337
1323
if seq:
1338
1324
1339
- No: if len(seq):
1340
- if not len(seq):
1325
+ No: if len(seq):
1326
+ if not len(seq):
1341
1327
1342
1328
- Don't write string literals that rely on significant trailing
1343
1329
whitespace. Such trailing whitespace is visually indistinguishable
@@ -1375,7 +1361,7 @@ annotations are changing.
1375
1361
- For code that wants to make a different use of function annotations
1376
1362
it is recommended to put a comment of the form::
1377
1363
1378
- # type: ignore
1364
+ # type: ignore
1379
1365
1380
1366
near the top of the file; this tells type checker to ignore all
1381
1367
annotations. (More fine-grained ways of disabling complaints from
@@ -1397,7 +1383,7 @@ annotations are changing.
1397
1383
can be added in the form of comments. See the relevant section of
1398
1384
PEP 484 [6]_.
1399
1385
1400
- Variable annotations
1386
+ Variable Annotations
1401
1387
--------------------
1402
1388
1403
1389
PEP 526 introduced variable annotations. The style recommendations for them are
@@ -1413,19 +1399,19 @@ similar to those on function annotations described above:
1413
1399
1414
1400
- Yes::
1415
1401
1416
- code: int
1402
+ code: int
1417
1403
1418
- class Point:
1419
- coords: Tuple[int, int]
1420
- label: str = '<unknown>'
1404
+ class Point:
1405
+ coords: Tuple[int, int]
1406
+ label: str = '<unknown>'
1421
1407
1422
1408
- No::
1423
1409
1424
- code:int # No space after colon
1425
- code : int # Space before colon
1410
+ code:int # No space after colon
1411
+ code : int # Space before colon
1426
1412
1427
- class Test:
1428
- result: int=0 # No spaces around equality sign
1413
+ class Test:
1414
+ result: int=0 # No spaces around equality sign
1429
1415
1430
1416
- Although the PEP 526 is accepted for Python 3.6, the variable annotation
1431
1417
syntax is the preferred syntax for stub files on all versions of Python
@@ -1460,7 +1446,6 @@ References
1460
1446
https://www.python.org/dev/peps/pep-0484/#suggested-syntax-for-python-2-7-and-straddling-code
1461
1447
1462
1448
1463
-
1464
1449
Copyright
1465
1450
=========
1466
1451
0 commit comments