Python keywords occupy grammatical roles, while identifiers bind names that must satisfy the parser’s naming rules.
Python keywords and identifiers: parser rules differ from field rules
Operation contract
The fixture compiles an owned assignment using a reserved keyword and checks that it fails before execution. A soft keyword remains usable as a name outside its special grammar position. The receipt identifier validator imposes an ASCII application grammar instead of assuming every valid Python identifier is a valid received field. No received text is executed.
Failure and ownership boundary
Isidentifier does not check whether a name is reserved, and the set of soft keywords depends on the interpreter release. Unicode identifiers are valid in source but may be inappropriate for a protocol that needs a fixed alphabet. Compilation can raise SyntaxError before any statement runs. Python Unicode normalization: equality is not visual identity and Python type conversion: parsing success is not field validity concern different boundaries.
Working program
import keyword
import re
print("reserved:", keyword.iskeyword("class"))
print("valid spelling:", "class".isidentifier())
namespace = {}
exec(compile("match = 125", "owned-source", "exec"), namespace)
print("soft keyword value:", namespace["match"])
try:
compile("class = 125", "owned-source", "exec")
except SyntaxError:
print("reserved assignment rejected")
print("wire accepted:", re.fullmatch(r"[A-Z][A-Z0-9_]{0,15}", "RECEIPT_41") is not None)Output
reserved: True
valid spelling: True
soft keyword value: 125
reserved assignment rejected
wire accepted: TrueCosts and limits
Identifier scanning depends on source length. This fixture compiles fixed owned strings and validates a bounded field. Compiling received program text would introduce an execution boundary that this lesson does not provide.
Common Mistakes
- A valid identifier can still be reserved.
- Do not execute received text to validate a field.
Connected lessons
Python interpreter and virtual environments: run the intended executable, Python type conversion: parsing success is not field validity, Python Unicode normalization: equality is not visual identity.
