Python Classes
MIT 6.100L Intro to CS and Programming using Python, lecture 17, explained in plain English
Want this lecture to stick? In LessonBound you get checked practice questions for every part, can ask about anything you don't get, and can listen with your screen off. Free, no ads.
Learn this lecture freeWatch or read the original lecture: MIT OpenCourseWare.
Part 1 · 0:00–10:00
This part introduces Python classes, the way programmers design their own kinds of objects by deciding what data each object holds and what it can do.
Key ideas
- Over the next four lectures, the goal is to define your own object types using Python classes. This lecture starts with one simple type. 0:38
- Every object is built from a blueprint. When you design a new object type, you decide two things: what data represents the object, and what behaviors it has. 2:06
- An instance is one specific object made from a type. For example, the number 1234 is an instance of an integer. 3:30
- A car blueprint is a design choice. You might pick length, width, height, and color as the data. Someone else could pick different data and still make a valid car. 5:57
- The interface is the set of ways other programmers or objects can use an object, such as changing its color, honking the horn, or driving from point A to point B. 6:41
Words to know
- instance
- One specific object that was made from a type or blueprint, like one particular car rather than the idea of cars in general.
- blueprint
- A plan that says how to build a certain kind of object. The plan itself is not the object, just the instructions for making it.
- interface
- The list of things you can do with an object, or ways other code can work with it. It is like the buttons and controls on a device.
- data abstraction
- Deciding which pieces of information represent an object, and hiding the messy details of how that information is stored.
Example
Think of two real cars that were both built from the same car blueprint. The blueprint is not a car. It is the plan that says what each car should have, like length, width, height, and color. Each real car is an instance of that blueprint.
3 checked practice questions for this part are in the app.
Part 2 · 10:00–20:00
This part explains how Python classes bundle data and behaviors into one object, using queues, stacks, and a 2D coordinate as examples.
Key ideas
- Object-oriented programming puts data and behaviors together in one thing. Every object of the same type holds data in the same way and works in the same way. 12:55
- A queue and a stack can both be stored as a list. The difference is the rule for taking items out: a queue is first in, first out, and a stack is last in, first out. 11:27
- You decide the design of a class. You choose what data represents it and what behaviors it has, and different choices can still make the same kind of object. 13:35
- Defining a class is like writing a function definition: it describes the object but does not make one yet. Creating instances of the class is like calling the function, and it gives you real objects to use. 16:49
- A point on a 2D plane can be stored as two numbers, one for its x distance and one for its y distance. Its behaviors include reporting those distances and finding the Euclidean distance between two points. 18:14
Words to know
- class
- A blueprint that tells Python what data an object holds and what it can do.
- instance
- One real object made from a class, such as one particular queue or one particular point.
- Euclidean distance
- The straight-line distance between two points on a flat plane.
- first in, first out
- A rule where the item that went in first is the first one taken out, like a line at a store.
Example
Think of a line at a store. People join at the back and leave from the front, so the first person in line is the first one served. A queue class works the same way: it stores the names in a list, and its behavior makes sure the oldest name is always removed first.
3 checked practice questions for this part are in the app.
Part 3 · 20:00–30:00
This part shows how to start building your own Python data type, called a class, with a setup function and a special word called self.
Key ideas
- The keyword class tells Python you are creating a new type of object, such as coordinate. At this point no actual objects exist yet. 20:23
- An object is made of two things: data that describes it, and behaviors, which are functions that work with that object. For a coordinate, the data is two numbers, x and y. 21:49
- Every class needs a special setup function called __init__, which says how a new object should be built. Its extra inputs, such as x and y, must be given when you create the object. 23:12
- The word self is used because no real object exists yet while the class is being written. It is a placeholder that refers to an object of this type. 24:15
- Writing self.x instead of plain x keeps the value around for as long as the object lives. A plain x disappears when the setup function finishes. 27:06
Words to know
- method
- A function that only works on objects of one particular type, such as a distance function that only works on coordinates.
- self
- A placeholder name that refers to the object being built or used. It is always the first input in a method.
- instance
- One real object made from a class blueprint, like one specific room built from a room plan.
- __init__
- The special setup function that runs when a new object is created. Its name has double underscores on each side.
Example
Think of a room blueprint that says a room has two chairs, a coffee table, and a sofa. While the plan is still just an idea, you refer to its parts as self.coffeetable. Once you build a real living room from the plan, you refer to its table as livingroom.coffeetable.
3 checked practice questions for this part are in the app.
Part 4 · 30:00–40:00
This part explains how to turn a class blueprint into real objects, how those objects store their own data, and how to write a method that works on an object.
Key ideas
- A class is only a blueprint. Defining it does not create any objects. Objects appear only when you use the blueprint to build one. 30:20
- You create an object by writing the class name and passing in every parameter except self. Python fills in self with the new object it is making. 31:04
- Values saved as self.x and self.y stay with the object for as long as it exists. A plain variable inside the setup code would disappear once that code finished. 33:11
- A method is a function that works on an object. You make one by putting self as the first parameter, so the method knows which object it is working on. 37:55
- Python does not check what type of value you pass into a method. Passing the wrong type, such as an integer where a coordinate is expected, may run at first but then crash inside the method. 39:43
Words to know
- instance
- One real object built from a class blueprint. Each instance can hold its own values, like two different living rooms built from the same plan.
- constructor
- The special setup code, called the init method, that runs when you create a new object and sets up its starting values.
- dot notation
- Writing a name, then a dot, then another name, such as c.x, to get a value that belongs to an object.
- method
- A function that belongs to a class and works on an object of that class.
Example
Think of a house plan that is used to build many houses. Each house built from the plan is an instance, and each can have its own paint color and furniture. The plan itself is not a house, just like a class is not an object until you build one from it.
3 checked practice questions for this part are in the app.
Part 5 · 40:00–47:34
This part shows how a method works on a Coordinate object, using a distance method as the example, and how the dot notation calls it.
Key ideas
- The distance method finds the gap between two coordinates. It subtracts the x values, squares the result, does the same for y, adds the two, and takes the square root. 40:19
- A method is a function that belongs to an object. It looks like a normal function, but its first input is self, which stands for the object itself. 41:23
- You call a method with dot notation: the object goes before the dot, the method name goes after it, and any extra values go in brackets. Python fills self with the object before the dot, so you leave it out when you call the method. 43:31
- You can also call a method through the class name, but then you must pass self yourself as the first value. This shows that the shorter dot form is just a shortcut for that longer call. 45:45
Words to know
- method
- A function that belongs to an object. You call it on that object using a dot.
- dot notation
- Writing the object, then a dot, then the name of a method or data item, such as c.distance.
- self
- The name used inside a class for the object the method is working on. Python fills it in for you when you call the method.
- parameter
- A value you pass into a function or method through the brackets, so it can use that value.
Example
Think of a coffee machine with a brew button. You press brew on one specific machine, and that machine is the one that makes the coffee, so you do not have to tell it which machine to use. The object before the dot works the same way: it is the thing that does the work.
3 checked practice questions for this part are in the app.
← Lecture 15: Recursion · Lecture 18: More Python Class Methods →
Notes written by AI (Claude) from the lecture's official transcript, so check anything important against the lecture. MIT OpenCourseWare · CC BY-NC-SA 4.0 · not affiliated with or endorsed by MIT. These notes are shared under the same license.
LessonBound · Free lecture notes · Privacy · Terms · New York