Skip to content
Tactiverge

Computer engineering · also RoboticsLevel 3 · Innovator

Classes and objects

A class is a blueprint that bundles data (attributes) with the functions that act on it (methods). Each object made from the class is an instance with its own attribute values, so two range sensors built from one class can hold different offsets and separate lists of readings. A special set-up method, init, runs for every new instance, and self is the instance a method is working on. Attributes defined on the class itself are shared by every instance, which is useful for constants but a classic bug for mutable lists. Objects are clearest when data and behaviour belong together and a rule must always hold, such as a motor that never accepts a speed above its limit; plain functions stay clearer for one-off calculations.

Why it mattersA robot's software can model each motor, sensor and map as an object that guards its own limits, so one fix to the class reaches every instance.

Learn it

  1. 1One blueprint, many sensorsTactiverge School · Level 3
  2. 2Objects that guard their own limitsTactiverge School · Level 3
  3. 3Object or function? Choosing the clearer designTactiverge School · Level 3