Engineering Python · Functions & Files
Light OOP: Shapes
In Engineering Python because a few labs group data with the formulas that belong to it — a class is that grouping, not a second language.
A class names a kind of object. An instance holds that object's data. A method is a function that receives the instance as `self`. Inheritance lets Circle, Triangle and Square reuse a Shape idea and fill in their own area and perimeter — one pattern, not a design-patterns course.
- Engineering Python
- Medium level
- 4 concepts
1Class, instance, self
A class is a blueprint. Calling `Square(3)` builds one instance whose `side` is 3. Methods are functions defined inside the class; the instance is passed as `self` so they can read that instance's data.
This is still ordinary Python: `__init__` stores fields, and other methods compute from those fields. You already know functions; `self` is the extra first argument.
Figure. A class is a blueprint. Square(3) builds one instance whose side is 3. area reads self.side and returns self.side * self.side, which is 9. s.area() passes s as self.
One class, one instance
class Square:
def __init__(self, side):
self.side = side
def area(self):
return self.side * self.side
s = Square(3)
print(s.area())In `def area(self):`, what is `self`?
- The instance the method was called on
- A global list of all squares
- The class name as a string
`s.area()` passes s as self.
2Inheritance: one idea, specialised formulas
A subclass written `class Circle(Shape):` is a Shape and can add or replace methods. The shared idea here is "this object can answer area and perimeter"; each shape supplies the formula.
You do not need abstract base classes, private fields, or multiple inheritance for this lab. One parent, a few children, two methods.
Figure. A subclass written class Circle(Shape): is a Shape and can add or replace methods. The shared idea is that the object can answer area and perimeter; Square fills side*side and 4*side. You do not need abstract base classes, private fields, or multiple inheritance.
Takeaway
- IdeaA subclass written `class Circle(Shape):` is a Shape and can add or replace methods. The shared idea here is "this object can answer area and perimeter"; each shape supplies the formula.
Shape then Square
class Shape:
def area(self):
return 0.0
def perimeter(self):
return 0.0
class Square(Shape):
def __init__(self, side):
self.side = side
def area(self):
return self.side * self.side
def perimeter(self):
return 4 * self.side3Circle, triangle, square formulas
Square: area is side*side; perimeter is 4*side. Circle: area is pi*r*r; perimeter (circumference) is 2*pi*r — use `math.pi`. Triangle: area is 0.5*base*height; perimeter is the sum of the three sides.
The class only stores the numbers the formula needs. A circle stores r; a triangle stores base, height, and the three sides if perimeter is required.
No diagram — the idea is carried by the prose, code block or coding lab.
| Shape | Area | Perimeter |
|---|---|---|
| Square | side*side | 4*side |
| Circle | pi*r*r | 2*pi*r |
| Triangle | 0.5*base*height | side sum |
Square of side 3
A Square instance with side 3. What are area and perimeter?
- area = 3 * 39
- perimeter = 4 * 312
Pro tip. Same numbers as s.area() and s.perimeter() after Square(3).
4Lab: square area and perimeter
Define Square with area and perimeter, construct side 3, and print both results.
Figure. Square(3) stores side 3. area is side*side = 9. perimeter is 4*side = 12. Methods read self.side — the check can see both 9 and 12.
Takeaway
- IdeaDefine Square with area and perimeter, construct side 3, and print both results.
Coding lab. Square methods runs in the app, with checks on your output.
Notes
- In Engineering Python because a few labs group data with the formulas that belong to it — a class is that grouping, not a second language.
- A class is a blueprint. Calling `Square(3)` builds one instance whose `side` is 3. Methods are functions defined inside the class; the instance is passed as `self` so they can read that instance's data.
- A subclass written `class Circle(Shape):` is a Shape and can add or replace methods. The shared idea here is "this object can answer area and perimeter"; each shape supplies the formula.
Exam traps & shortcuts
- Run one cell at a time and read stdout before changing more lines.
- Names are labels for values; rebinding a name does not rewrite old prints.
Recap
A class groups data with methods; subclasses specialise area and perimeter.
- Class, instance, self
- A class is a blueprint. Calling `Square(3)` builds one instance whose `side` is 3. Methods are functions defined inside the class; the instance is passed as `self` so they can read
- Inheritance: one idea, specialised formulas
- A subclass written `class Circle(Shape):` is a Shape and can add or replace methods. The shared idea here is "this object can answer area and perimeter"; each shape supplies the fo
- Circle, triangle, square formulas
- Square: area is side*side; perimeter is 4*side. Circle: area is pi*r*r; perimeter (circumference) is 2*pi*r — use `math.pi`. Triangle: area is 0.5*base*height; perimeter is the sum
- Lab: square area and perimeter
- Define Square with area and perimeter, construct side 3, and print both results.
Practise Light OOP: Shapes
Reading is free and needs no account. Practice, mocks and progress live in the app.
- A 2-question practice set that ends the chapter
- 1 quick check with worked explanations
- Timed mocks scored with the real marking scheme
- Readiness tracked per topic, kept on your device