Week 8

Object-oriented programming

A class bundles data and the actions that belong with that data. Each object made from the class remembers its own data.

A class is a blueprint. An object is one thing made from it. Attributes are the data that object remembers. Methods are functions that belong to the object, and they receive the object itself as self. __init__ runs when you create the object and is where you set the starting attributes.

__str__ decides what the object looks like when printed. A property lets you read or write an attribute through a method, so you can reject a bad value. A class method belongs to the blueprint rather than one object. Inheritance is a new class that starts from an old one and changes a few things.

A fresh example: a class Lamp with a brightness attribute and a method that dims it, and that refuses a brightness below zero. Make two lamps and dim only one. The other stays as it was. That is the point of separate objects.

The idea
An object holds its own data one object its data a value

Problems