-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path127_code_convention_python.py
More file actions
101 lines (54 loc) · 2.19 KB
/
127_code_convention_python.py
File metadata and controls
101 lines (54 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#TABS: Tabs are very important in python
#GOOD
def my_function1():
x = []
if x:
print("Hello, World!")
#BAD
def my_function():
x = []
if x:
print("Hello, world!")
#Maximum Line Length: Limit lines to 79 characters for code and 72
# for docstrings and comments. However, the more modern recommendation
# is to limit lines to 88 characters, as it works well with modern text editors and widescreen displays.
# Good
def long_function_name1(parameter1, parameter2, parameter3,
parameter4, parameter5):
print("HI")
# Code goes here...
# Bad (exceeds line length limit)
def long_function_name(parameter1, parameter2, parameter3, parameter4, parameter5):
# Code goes here...
print("hi")
#Imports: Imports should usually be on separate lines and grouped in the following order:
# Standard library imports
# Related third-party imports
# Local application/library specific imports
# Good
import os
import sys
from my_module import my_function
# Bad (unorganized or multiple imports on one line)
import os, sys
from my_module import *
#Whitespace in Expressions and Statements: Avoid extraneous whitespace in the following situations:
# Avoid whitespace immediately before a comma, semicolon, or colon.
# Avoid whitespace immediately before an open parenthesis that starts an argument list, indexing, or slicing.
# Avoid whitespace immediately before the open parenthesis that starts a function call.
# Avoid whitespace immediately before the open parenthesis that starts an indexing or slicing.
# Good
spam(ham[1], {eggs: 2})
# Bad (extraneous whitespace)
spam( ham[ 1 ], { eggs: 2 } )
#Comments: Use comments sparingly and focus on explaining why something is done,
# not what it does (unless it’s particularly complex). Use docstrings to document classes, functions, and modules.
# Good:
def calculate_total1(price, tax_rate):
"""Calculate the total cost given a price and tax rate."""
return price * (1 + tax_rate / 100)
# Bad (excessive or unclear comments):
def calculate_total(price, tax_rate):
# This function calculates the total cost
# Based on the given price and tax rate
return price * (1 + tax_rate / 100)