Classes
A class is a blueprint for objects that carry their own data. Step through counter.py: class Counter: builds one class object holding two methods; calling Counter(5) makes a new object and __init__ fills in its count; c.add(2) finds add on the class and changes that one object from 5 to 7; a second object keeps its own count. Finish with the mistake every newcomer makes once: forgetting self.
5 quick questions
Pick an answer to see why. Your score saves with your progress.
01Python runs class Dog: followed by an indented body of methods. What exists afterwards?
Running a class statement builds one class object and binds it to the name Dog. No instance exists until you call Dog(...).
02p = Point(1, 2) runs. What happens right after Python makes the new, empty object?
Calling a class creates an instance and then calls __init__ on it automatically: self is the new object, and the arguments 1 and 2 follow.
03c is a Counter object. Which call is c.add(2) the same as?
Python finds add on the class and passes the object itself as the first argument, self. So c.add(2) runs Counter.add(c, 2).
04a = Counter(1); b = Counter(1); a.add(5). What is b.count now?
a and b are separate instances, each with its own count attribute. Changing a's count leaves b's at 1.
05A class has def add(n): (no self). What does c.add(2) do?
Python always passes the object as the first argument, so add receives c and 2: two arguments for one parameter. Every method needs self (or some name) for that first argument.
Glossary
- Class
- A blueprint for objects. Running a class statement builds a class object that holds the methods defined inside it.
- Instance (object)
- One object made from a class, by calling the class like a function, such as Counter(5).
- Attribute
- A name stored on an object, read and written with a dot: c.count.
- Method
- A function defined inside a class. Called on an object, it receives that object as its first argument.
- self
- The usual name for a method's first parameter: the object the method was called on.
- __init__
- The method Python calls automatically on each new instance to set up its attributes.
- State
- The values an object holds at a moment. Each instance keeps its own.
Notes and sources
- On macOS and Linux the command may be python3 rather than python; on Windows, py also works. The $ is the terminal's prompt: you do not type it.
- PEP 8, the Python style guide, puts a blank line between methods and two blank lines around a class. The figure leaves them out to fit the card; Python runs the code the same way.
- self is only a convention: Python passes the object as the first argument whatever that parameter is called. Every Python programmer still calls it self.
- __init__ does not build the object. Python creates the empty instance first (with __new__) and then hands it to __init__ to fill in; __init__ must return None.
- When you read an attribute, Python checks the object's own attributes first, then its class (and then any classes that class inherits from).
- The figure shortens the traceback: it leaves out the lines that name the file and show line 7, and wraps the long last line onto two rows. Python 3.10 and later name the method as Counter.add().
- Python tutorial: Classes
- Python tutorial: A first look at classes (class and instance objects, method objects)
- Python reference: Class definitions
- Python reference: Data model, object.__init__
- Python reference: Data model, class instances (attribute lookup)
- Python library: Built-in functions (isinstance, type, vars)
- Python glossary: method
- PEP 8: Style Guide for Python Code, blank lines
Quiz Unlocks after the last step.