Choose variable, parameter, field, and class names that clearly communicate their purpose and context. Favor specific, self-explanatory identifiers over generic or ambiguous ones, and ensure they follow Python's snake_case convention.
Choose variable, parameter, field, and class names that clearly communicate their purpose and context. Favor specific, self-explanatory identifiers over generic or ambiguous ones, and ensure they follow Python’s snake_case convention.
When naming variables:
func
, type_str
, or dict1
that don’t explain their purposeBad:
def serialize_rpc_event(self, event, group_cache, additional_attributes):
attributeDict = {
attribute: event[attribute]
for attribute in additional_attributes
if attribute in event
}
def _assemble_preprod_artifact(assemble_task, project_id, org_id, checksum, chunks, func):
# func is too generic and unclear what type of function is expected
Good:
def serialize_rpc_event(self, event, group_cache, additional_attributes):
attribute_dict = {
attribute: event[attribute]
for attribute in additional_attributes
if attribute in event
}
def _assemble_preprod_artifact(assemble_task, project_id, org_id, checksum, chunks, callback):
# callback clearly indicates the expected function's purpose
For class naming, ensure the name reflects the primary purpose:
# Instead of MultiProducerManager for a class that mainly acts as a producer
class MultiProducer:
def produce(self):
# implementation
When related but distinct concepts exist, use clear differentiators in names:
# Instead of ambiguous:
data_source_id = data_packet.source_id
# Use more specific naming:
data_packet_source_id = data_packet.source_id # Avoids confusion with DataSource.id
Enter the URL of a public GitHub repository