teluzia.Cloud Engineer · Day 29
CHAPTER 05 · PYTHON AUTOMATION

OOP fundamentals

4–5 hours including independent practice3 topics
LET’S MAKE THIS SIMPLE

What you’re learning today

Model a small monitoring reading as an object and decide whether the object adds clarity. Object-oriented programming groups state and behavior, but a simple function can remain the better choice for a stateless transformation. You will create independent instances and deliberately test a policy boundary. A class defines behavior and attributes for its instances. __init__ initializes a newly created instance’s state; self refers to the instance receiving a method call. Different instances should have independent per-instance data. A method can read and update that state or compute a result from it according to a documented contract. Encapsulation exposes a useful interface while controlling assumptions about state. Python underscores indicate convention rather than enforced secrecy. A constructor or property can validate values so every instance starts in a meaningful state. An invariant is a condition expected to remain true across supported operations, such as a percentage staying between zero and one hundred.

Think of it this way

A class is a blueprint; an instance is one actual device built from it, carrying its own readings and behavior.

TOPIC 01 / 03Step by step

Classes and instances

A class defines behavior and attributes for its instances. __init__ initializes a newly created instance’s state; self refers to the instance receiving a method call. Different instances should have independent per-instance data. A method can read and update that state or compute a result from it according to a documented contract. Reading("api",80) and Reading("worker",40) should retain different names and values; changing one instance’s value should not automatically change the other instance’s stored state in this small model. Bundling related state and behavior makes repeated operations on the same entity clearer and helps prevent passing mismatched identifiers and values between unrelated functions as an application grows beyond its first simple prototype. Putting mutable per-instance state in a class-level list shares it across instances; initialize instance collections inside __init__ unless shared state is explicitly intended and documented as part of the class’s actual design contract.

WORKED EXAMPLE
Reading("api",80) and Reading("worker",40) should retain different names and values; changing one instance’s value should not automatically change the other instance’s stored state in this small model.
Why this matters

Bundling related state and behavior makes repeated operations on the same entity clearer and helps prevent passing mismatched identifiers and values between unrelated functions as an application grows beyond its first simple prototype.

A common trap

Putting mutable per-instance state in a class-level list shares it across instances; initialize instance collections inside __init__ unless shared state is explicitly intended and documented as part of the class’s actual design contract.

Read all topics in this lesson

A complete reference to revisit alongside the guided steps above.

Classes and instances

A class defines behavior and attributes for its instances. __init__ initializes a newly created instance’s state; self refers to the instance receiving a method call. Different instances should have independent per-instance data. A method can read and update that state or compute a result from it according to a documented contract. Reading("api",80) and Reading("worker",40) should retain different names and values; changing one instance’s value should not automatically change the other instance’s stored state in this small model. Bundling related state and behavior makes repeated operations on the same entity clearer and helps prevent passing mismatched identifiers and values between unrelated functions as an application grows beyond its first simple prototype. Putting mutable per-instance state in a class-level list shares it across instances; initialize instance collections inside __init__ unless shared state is explicitly intended and documented as part of the class’s actual design contract.

WORKED EXAMPLE
Reading("api",80) and Reading("worker",40) should retain different names and values; changing one instance’s value should not automatically change the other instance’s stored state in this small model.

Why this matters

Bundling related state and behavior makes repeated operations on the same entity clearer and helps prevent passing mismatched identifiers and values between unrelated functions as an application grows beyond its first simple prototype.

A common trap

Putting mutable per-instance state in a class-level list shares it across instances; initialize instance collections inside __init__ unless shared state is explicitly intended and documented as part of the class’s actual design contract.

Encapsulation and invariants

Encapsulation exposes a useful interface while controlling assumptions about state. Python underscores indicate convention rather than enforced secrecy. A constructor or property can validate values so every instance starts in a meaningful state. An invariant is a condition expected to remain true across supported operations, such as a percentage staying between zero and one hundred. A constructor that rejects value<0 or value>100 with ValueError prevents impossible percentage readings; underscore-prefixed attributes alone would not perform that validation or protect data from accidental misuse by a caller. Validated invariants reduce repeated defensive checks in consumers and make invalid states easier to diagnose at their origin instead of allowing impossible values to propagate into reports, thresholds and later automated decisions throughout a larger system. Assuming a leading underscore encrypts or makes an attribute inaccessible misunderstands Python conventions; validate meaningful state and document supported interfaces rather than relying on naming alone as a security boundary for sensitive values in memory.

WORKED EXAMPLE
A constructor that rejects value<0 or value>100 with ValueError prevents impossible percentage readings; underscore-prefixed attributes alone would not perform that validation or protect data from accidental misuse by a caller.

Why this matters

Validated invariants reduce repeated defensive checks in consumers and make invalid states easier to diagnose at their origin instead of allowing impossible values to propagate into reports, thresholds and later automated decisions throughout a larger system.

A common trap

Assuming a leading underscore encrypts or makes an attribute inaccessible misunderstands Python conventions; validate meaningful state and document supported interfaces rather than relying on naming alone as a security boundary for sensitive values in memory.

Composition and inheritance

Composition builds a larger object from collaborating objects; inheritance expresses a specialized subtype relationship. Prefer a small explicit design when behavior differs only slightly. Overriding methods should preserve the expected contract of callers. OOP is useful for sustained stateful entities, not a requirement that every ten-line script needs a hierarchy. A Monitor containing several Reading objects is composition; a specialized Reading subclass is justified only when it still behaves like a Reading while adding meaningful specialized behavior for its callers rather than hiding incompatible assumptions. Choosing composition or a simple function deliberately keeps automation understandable and avoids fragile hierarchies whose interactions are harder to test than the original problem they were introduced to organize for users and maintainers of a small project. Creating inheritance merely to reuse two lines couples unrelated concepts; extract a helper or compose behavior unless a genuine subtype relationship and compatible caller contract make the inheritance understandable and valuable to the program being designed.

WORKED EXAMPLE
A Monitor containing several Reading objects is composition; a specialized Reading subclass is justified only when it still behaves like a Reading while adding meaningful specialized behavior for its callers rather than hiding incompatible assumptions.

Why this matters

Choosing composition or a simple function deliberately keeps automation understandable and avoids fragile hierarchies whose interactions are harder to test than the original problem they were introduced to organize for users and maintainers of a small project.

A common trap

Creating inheritance merely to reuse two lines couples unrelated concepts; extract a helper or compose behavior unless a genuine subtype relationship and compatible caller contract make the inheritance understandable and valuable to the program being designed.

KEEP THESE WITH YOU

Today’s takeaways

Use objects for coherent state and behavior.

Validate invariants explicitly.

Prefer simple composition over unnecessary hierarchies.

CLOSE THE LOOP

Ready to complete this day?

Verify the practical steps and pass the knowledge check (at least two thirds correct).

Lab steps verifiedKnowledge check passed