liveBook · Manning

Const qualifier

As explained in Practices of the Python Pro

A language feature that marks a value, binding, or access path as constant, restricting reassignment or modification according to the language’s rules.

A constant is a value or binding intended to remain unchanged within a computation or defined scope. Giving repeated data one name makes a design easier to update and reduces the chance of inconsistent edits. section 1.3.1 section 1.7.1

One delimiter, many output lines

Instead of repeating a comma in every output expression, define DELIMITER once and reuse it: DELIMITER = '\t'; then join each row with DELIMITER. Switching back to commas requires changing one assignment rather than searching through several lines. section 1.3.1

DELIMITER = '\t'
print(DELIMITER.join([col1_name, col2_name, col3_name, col4_name]))
print(DELIMITER.join([first_val, second_val, third_val, fourth_val]))

Compare repeated separators with a named delimiter and join-based output. section 1.3.1

Precomputing reusable data

State capitals can be calculated once and stored for later use: US_CAPITALS_BY_STATE holds the mapping, while US_CAPITALS stores its sorted values. This avoids repeating the same calculation without sacrificing readability. section 1.7.1

Listing 1.3. Refactored code reveals a more concise solution
US_CAPITALS_BY_STATE = {'Alabama': 'Montgomery', ...}    #1
US_CAPITALS = sorted(US_CAPITALS_BY_STATE.values())      #2

See the refactoring from a reusable function to module-level data computed once. section 1.7.1

How Practices of the Python Pro covers it

How deep it goes
Demonstrates constants through short Python refactorings but stops short of discussing immutability or language-level enforcement.
Examples
Uses CSV-to-TSV delimiters and alphabetized United States state capitals as practical examples.
What you can do after
Apply it via named Python values to reduce duplicated edits and repeated calculations.
How it connects
Connects constants with refactoring, code reuse, readability, and performance.

Also explained in

Compare how 7 books explain const qualifier →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Practices of the Python Pro. The section links point to the parts of the book it draws on.