Python Classes and Objects
PCAP objective 4.1 is understand the object-oriented approach, and it is the start of the 34% of that exam PCEP never mentions. A class is a template; an instance is a thing made from it. Both are objects, both have a __dict__, and attributes can be added to an instance from outside the class entirely - which is unusual, and which several later questions depend on.
Object-Oriented Python Guide 12 of 25 Intermediate
- OSUbuntu 26.04 LTS
- Python3.14.4
- pip25.1.1
- TimeAbout 16 min
Every class in Python 3 is a new-style class inheriting from object, whether you say so or not - class Dog: and class Dog(object): are identical. Older material that distinguishes them is describing Python 2.
| Server Name | IP Address | OS | Roles | CPU | RAM | HDD |
|---|---|---|---|---|---|---|
| RUNNER01 | 192.168.0.27 | Ubuntu 26.04 LTS | Python 3.14.4 - the only machine this path needs | 2 Core | 4 GB | 50 GB |
This guide includes
Use this for the object-oriented vocabulary objective. This matters because Python lets you add an attribute from outside the class - the part that surprises everyone arriving from a stricter language.
- writing the smallest class
- adding attributes from outside, and asking for one that is not there
- seeing what an object prints as by default
- controlling that with
__str__and__repr__ - putting data and behaviour together in one class
Before you start
-
The smallest class
-
Attributes can be added from outside
-
Asking for an attribute that is not there
-
What an object prints as by default
-
__str__ and __repr__
-
A class with data and behaviour
-
What the exam does with this objective