Pcornel
Automation · Python Automation

Classes

Cheat sheet · https://pcornel.dk/topic.php?topic=5260

Back to the Topic

Answer

Python classes bundle data and methods into objects, such as a Device class with connect() and backup() methods, making larger automation easier to structure and test.

Remember

  • Classes bundle data and methods.
  • They make larger automation easier to test.
  • Avoid classes for trivial scripts.

First checks

  • python3 -c 'import m; help(m.MyClass)' — class docs
  • mypy . — type checking
  • Dataclasses/pydantic models — data structures

Verify in this order

class → instances → methods

Where it usually breaks

Input/desired state → request accepted → job/task terminal status → target object read-back → effective runtime state → original workload → regression/rollback readiness.

Python Automation → Classes · operational reference

LayerPython Automation → Classes operational reference
Runtime/ownershipPython class-based automation code · the Python interpreter and object model binding class definitions, instances, attributes, methods and inheritance at runtime
Persistent intentversion-controlled class contracts defining constructor inputs, instance state, public methods, invariants and composition/inheritance boundaries
VerificationInstantiate the class with a deterministic fixture, assert instance/class attribute behavior and method results, then exercise one mocked or sandboxed external side effect and verify the target separately.
Expected interpretationInstances preserve their own valid state, inheritance/composition resolves the intended methods, failures surface at the correct boundary, and any external mutation is attributable and independently verifiable.
Recovery boundarycapture inputs and object/external IDs, reproduce with a minimal instance, fix the class contract at the narrowest layer, then rerun unit/fixture tests and independent external read-back

Read-only verification commands unless stated otherwise. Confirm syntax for your platform and release. Full reference: https://pcornel.dk/topic.php?topic=5260

Confirmation

Confirm action

Continue?