---
title: "Classes and Objects: Bundling Data with Behaviour"
description: "A class defines a kind of object, with attributes for its data and methods for its behaviour. Each instance can hold its own state. An initializer sets starting values; access rules require deliberate"
canonical: https://lightmysky.com/learn/computing/classes-and-objects-bundling-data-with-behaviour-mt_kRvg7LZ_kk
source: https://lightmysky.com/learn/computing/classes-and-objects-bundling-data-with-behaviour-mt_kRvg7LZ_kk.md
retrieved: 2026-11-12
---

> **Agent view.** This is the Markdown twin of the page, for tools and assistants.
> When to use this site, and the call that answers each job: https://lightmysky.com/agent-instructions.md
> API description (OpenAPI 3.1): https://lightmysky.com/openapi.json · Authentication: https://lightmysky.com/auth.md
> Pricing: https://lightmysky.com/pricing.md · Catalog: https://lightmysky.com/llms.txt · Full catalog: https://lightmysky.com/llms-full.txt
> Every machine-readable file on this domain: https://lightmysky.com/.well-known/ai-catalog.json
> Ask for Markdown with `Accept: text/markdown`, a `.md` address, or `?mode=agent`.

# Classes and Objects: Bundling Data with Behaviour

A class defines a kind of object, with attributes for its data and methods for its behaviour. Each instance can hold its own state. An initializer sets starting values; access rules require deliberate design.

Subject: Computing · Area: Programming · Ages 16 to 18
Page: https://lightmysky.com/learn/computing/classes-and-objects-bundling-data-with-behaviour-mt_kRvg7LZ_kk

## Ready when they can

- Define a class with a constructor and two methods, then create two objects from it
- Say what belongs to the object and what belongs to the class
- Rewrite a loose group of variables and functions as one class, and say what improved

## Lesson: Blueprints that build their own copies

*(drawing: A class defines attributes and methods. Calling it creates an instance when its initializer accepts the supplied arguments.)*

**Example.** __init__ initializes an already-created instance. self refers to that instance.
```python
class Account:
    def __init__(self, balance):
        self.balance = balance
    def deposit(self, amount):
        self.balance += amount
    def withdraw(self, amount):
        self.balance -= amount
mia = Account(100)
sam = Account(40)
```

In obj.area(), Python supplies obj as self. A method returning self.length * self.width multiplies that object's dimensions. With length 3 and width 7, it returns 21.

An instance attribute belongs to one object; a class attribute belongs to the class. Instances read CoffeeOrder.loc unless they shadow it with an instance attribute of the same name. Assigning pet_1.name does not assign pet_2.name. Referenced mutable values may still be shared.

**Example.** An instance method can update its receiver:
```python
def update_max(self, speed):
    self.max_mph = speed
```
For car.update_max(90.0), self is car. Other cars receive no assignment.

Start CoffeeOrder.cls_id at 0. Each initializer first adds 1 to CoffeeOrder.cls_id, then assigns self.order_id = CoffeeOrder.cls_id. The first two creations receive IDs 1 and 2.

Account groups each balance with deposit and withdraw operations. It does not prevent outside assignment to public attributes. Rejecting invalid withdrawals requires deliberate checks; declaring a class does not supply them.

**Recap.** A class defines shared behaviour. Instances hold attributes, which may reference shared values. __init__ sets initial state; access rules need deliberate design.

*Class, attribute and method teaching adapted from the Introduction to Python Programming textbook corpus records, with independently written corrections and assessment repairs.*

## Practice

8 questions on this page, each with its working shown.

## Needs first

- [Dictionaries: Values Found by Key](https://lightmysky.com/learn/computing/dictionaries-values-found-by-key-mt_mofpj0n5AM)
- [Defining a Function with Parameters](https://lightmysky.com/learn/computing/defining-a-function-with-parameters-mt_pyo7yBg4H3)

## Opens up

- [Inheritance and Overriding Behaviour](https://lightmysky.com/learn/computing/inheritance-and-overriding-behaviour-mt_wBp7j_B6fj)
