Introduction to object-oriented programming
AdvancedA class is a blueprint, an object the real thing. __init__ sets starting attributes, self means the object itself; attributes hold data (s1.name), methods act (s1.introduce()). Classes tie data to behaviour, keeping code tidy and reusable.
- A class is the blueprint; objects are instances built from it — one class, unlimited objects.
- Attributes hold data (s1.name); methods act (s1.introduce()).
- __init__, the constructor, runs automatically at creation to set starting attributes.
文體小知識:編程教學課每個概念都配一段可以親手試的代碼例子,照住輸入、改一改,最快學會。
§Text Passage
1Object-oriented programming (OOP) bundles data and behaviour together. A class is the blueprint: it defines what data a 'student' holds and what a student can do; an object is a real thing built from that blueprint — Mary and John are each a Student object. One class can build unlimited objects.
2Every object has attributes — its data, such as name and class; and methods — the things it can do, such as introduce itself or average its marks. Creating an object automatically runs __init__ (the constructor), which sets the starting attributes; self inside a method means 'this very object', letting each object reach its own data.
3Define a Student class: under class Student:, write def __init__(self, name, cls): self.name = name, self.cls = cls. Build an object: s1 = Student("Mary", "3A"), then s1.name gives "Mary". Add a method: def introduce(self): print("I am " + self.name + " from " + self.cls); calling s1.introduce() prints 'I am Mary from 3A'.
4Why OOP? A class has dozens of students, each with a name, marks and actions — loose lists and variables get messy fast; a class ties 'student data' and 'student behaviour' together so code stays tidy and reusable. Game characters, phone apps and school systems are organised this way almost everywhere.
1Summary 重點摘要
A class is a blueprint, an object the real thing. __init__ sets starting attributes, self means the object itself; attributes hold data (s1.name), methods act (s1.introduce()). Classes tie data to behaviour, keeping code tidy and reusable.
2Vocabulary cards Vocabulary
The blueprint defining a kind of object's data and behaviour, e.g. a Student class.
An instance built from a class, holding its own attributes, e.g. Mary's Student object.
Data stored inside an object, e.g. s1.name, s1.cls.
A function belonging to the object, defined inside the class, e.g. introduce().
The special method run automatically when an object is created, setting starting attributes.
The first parameter of every method — the object being called — used to reach its own attributes.
Another word for object: one individual actually produced by a class.
The act of creating an object from a class, e.g. s1 = Student("Mary", "3A").
Keeping data and the methods that use it inside one class, hiding internal details.
3Step by step Steps
- Classes and objectsObject-oriented programming (OOP) bundles data and behaviour together for management. A class is the blueprint, defining what data a 'student' holds and what a student can do; an object is a real instance built from that blueprint — Mary and John are each a Student object. One class can build unlimited objects, just as one blueprint can raise countless flats. In reality, every message, contact and product inside a phone app is an object of some class.
- Setting attributes with __init__Attributes are the data an object carries. Define the __init__ method (the constructor) inside the class and Python runs it automatically at creation: def __init__(self, name, cls): self.name = name with self.cls = cls on the next line stores the passed "Mary" and "3A" inside the object. self means 'this very object', letting each object keep its own data. Parameter names are free, but self must come first — a fixed rule of Python.
- Building objects and reading attributesBuild an object with Student("Mary", "3A") stored in the variable s1. A dot then reaches inside: s1.name gives "Mary" and s1.cls gives "3A". Attributes change at any time: after s1.cls = "3C", reading s1.cls yields "3C". Objects never disturb each other: with s2 = Student("John", "3B"), editing s1's attributes leaves s2 untouched, because each object owns independent copies of its data — like two students each holding their own ID card.
- Methods: what an object can doA method is a function defined inside the class, representing what the object can do. After def introduce(self): print("I am " + self.name + " from " + self.cls), calling s1.introduce() prints 'I am Mary from 3A'. Grammar notes: the definition must include self, while the call passes nothing — Python fills s1 into self automatically. A method may read any of self's attributes, so every call works with the latest data and never goes stale.
- Methods computing from attributesMethods do more than display — they compute. Add def average(self): return sum(self.marks) / len(self.marks) inside the class, let s1.marks = [80, 90, 70], and calling s1.average() returns 80.0. Data and calculation live in the same object: once the marks attribute updates, average reflects the new numbers automatically with no extra maintenance. This is OOP's core advantage — whose data, whose behaviour — the student object managing its own marks and maths, keeping the program's structure clear.
- Why object orientation at allImagine handling a class of dozens: each student has a name, a class and a marks list, plus averaging and self-introductions. With loose lists and variables, names[3] and marks[3] soon drift out of alignment; a class ties 'student data' and 'student behaviour' together, one object per student, and the program becomes orderly at once. Change one line of the average method and every student object updates together. Game characters, school systems and shopping platforms are organised this way almost everywhere.
- A full example and the recurring errorsA full definition: under class Student:, __init__ stores the name and marks, and the average method follows. Build s = Student("Mary", [80, 90, 70]) and print(s.average()) gives 80.0. Three common errors: forgetting self in the method definition, which crashes; calling s1.introduce(s1) and doubling the argument, so Python takes s1 as a second parameter; and misspelling __init__ (two underscores on each side), so attributes never get set automatically. Avoid these one by one and the class runs cleanly.
4Diagrams Diagrams
5Worked example Example
Example: define a Student class (a name and a marks list), add an average() method, and average Mary's marks [80, 90, 70].
- Class and constructor: inside class Student:, def __init__(self, name, marks): self.name = name with self.marks = marks on the next line.
- Add the computing method: def average(self): return sum(self.marks) / len(self.marks), using the object's own marks attribute.
- Instantiate: s = Student("Mary", [80, 90, 70]) — the constructor stores both pieces of data.
- Call the method: print(s.average()); sum([80, 90, 70]) = 240 and 240 ÷ 3 = 80.0.
- Extension: build a second object Student("Peter", [60, 65]) whose average() gives 62.5, with the two objects never disturbing each other.
- Answer: s.average() returns 80.0, the mean of Mary's three marks — the class binds the data (marks) to the calculation (average), each object keeps its own list, and the method always uses that object's latest values.
6Common mistakes Mistakes
7Quick check Quick check
Q1Which is right about classes and objects? A. A class is the blueprint, an object an instance built from it B. An object is the blueprint, a class the instance C. They are identical
Q2Which method runs automatically when an object is created?
Q3After s1 = Student("Mary", "3A"), what is s1.name?
Q4In the definition def introduce(self):, what does self do?
Q5s1.marks = [70, 80] and average() returns sum(self.marks) / len(self.marks). What is s1.average()?
8Recap Recap
- A class is the blueprint; objects are instances built from it — one class, unlimited objects.
- Attributes hold data (s1.name); methods act (s1.introduce()).
- __init__, the constructor, runs automatically at creation to set starting attributes.
- self means the object itself: required in definitions, passed automatically in calls.
- Each object's attributes are independent; OOP ties data to behaviour, keeping code tidy and reusable.