Classes & Objects
Bundling data and the verbs that act on that data.
Prerequisites:FunctionsData Types
Story — the blueprint and the house
A blueprint isn't a house — it's a description of how to build one. A class is a blueprint. From it you can stamp out as many objects (houses) as you need. Each object has its own data — the rooms inside it — but they all share the same plan, the same set of methods.
Every method has an implicit first argument: "the object I'm being called on." That's self in Python, this in most other languages. When you write rex.learn("sit"), the runtime quietly hands rex to learn as self, so the method knows which object it's acting on.
Classa says
See it — fields + methods + identity
Three things matter: fields (the data each object owns), methods (the verbs that act on that data), and identity (each object is its own thing, even if its fields match another's). Two Dogs with the same name are still two dogs.
Classa says
Try it — would you make a class?
Exercise. Which of these earn a class, and which are better as a plain dict / record / tuple?
- (a) A 2D point with methods like
distance_to(other),translate(dx, dy). - (b) A row of CSV data with named columns and no behaviour.
- (c) A user account with
password_hash,set_password,verify.
Reveal answer
(a) class — there's real behaviour wrapped around the data. (b) plain record / namedtuple / dict — just fields, no verbs. (c) class — the methods that touch password_hash should live with it; an attacker shouldn't be able to read it from outside without going through verify.
Code it — five languages, one blueprint
Python and JavaScript spell self/this differently but mean the same. Java and C++ require a typed declaration for every field. Go takes a different shape entirely — structs and methods, no class keyword.
class Dog:
def __init__(self, name, breed):
self.name = name # data
self.breed = breed
self.tricks = []
def learn(self, trick): # method
self.tricks.append(trick)
def __repr__(self):
return f"Dog({self.name})"
rex = Dog("Rex", "labrador")
rex.learn("sit")
print(rex.name, rex.tricks) # Rex ['sit']Quiz it — make it stick
No Dumb Questions
Real questions other learners asked on this page.
What is "self" / "this"?
A reference to the current object. Methods need a way to talk about "the object I was called on," and that's self/this. Python makes self explicit; most other languages make it implicit.What's a constructor?
A method that runs when you create a new object. Its job is to set up the object's starting state — assign initial values to fields, validate inputs. Different languages spell it differently: __init__ (Python), constructor (JS), ClassName() (Java/C++/C#).What is inheritance?
A way to define a class as "X but with these changes." A Dog is-an Animal — it inherits all of Animal's methods, can override some, can add new ones. Composition (an object owning another object) is often a cleaner alternative — start with composition; reach for inheritance only when the "is-a" relationship is real and stable.