A naming standard gives program elements predictable, recognizable names. In Python code, consistent names help make scripts easier to read and standardize work shared with co-workers. PEP 8-style guidance distinguishes constants, variables, and functions rather than treating every name alike. section 3.1.1
Use names that communicate an element’s role and work with the surrounding tools:
- Write constants in uppercase, such as `NUM_STATES = 50`, instead of using a variable-style name for a fixed value. section 3.1.1
- Write functions in lowercase with underscores between words, such as `process_data`, rather than `PROCESSDATA`. section 3.1.1
- Do not reuse built-in names or keywords as variables or parameters. Replace an unclear or conflicting name such as `int` with a descriptive name such as `num_samples`; use `total` rather than `sum`. section 3.1.1
- Prefix PyTest test functions and test scripts with `test_` so PyTest can find and run them automatically in a directory. section 10.3
Name a data-processing function clearly
A customer-churn processing function is easier to understand when its name describes the action and its parameter describes the quantity being sampled: `def process_data(file_name, num_samples):` Here, `process_data` follows lowercase underscore naming, while `num_samples` avoids shadowing the built-in `int` and explains what the argument controls. section 3.1.1
Listing 3.5 Be clear with how you’re defining constants, variables, and functions. Following the PEP8 guidelines helps to make these common programming constructs more standardized across any code you or your co-workers may write.
def PROCESSDATA(file_name, int): #1
global df #1
df = pd.read_csv(file_name) #1
df = df.fillna(df.median()) #1
if int > 0: #1
df = df.sample(int) #1
def process_data(file_name, int): #2
global df #2
df = pd.read_csv(file_name) #2
df = df.fillna(df.median()) #2
if int > 0: #2
df = df.sample(int) #2
Compare the original all-caps function name with the lowercase, underscore-separated alternative for a data-processing workflow. section 3.1.1
Naming conventions are not only cosmetic. A test name can be an instruction to a tool: PyTest recognizes functions and files beginning with `test_`, allowing `pytest` to discover and execute matching tests without naming each one on the command line. section 10.3









