Creating Custom Classes
The class describes the kind of object and the object is the concrete value you work with. A single class can be associated with multiple objects.
From Blueprint to Object
A class is a blueprint or template, while an object is a concrete instance created from that blueprint. This distinction explains why values in Python have both data and behavior. When a variable is assigned a value, the value is an object of a particular class.
The class describes the kind of object and the object is the concrete value you work with. A single class can be associated with multiple objects.
Tracing Assignment
Consider the assignment i = 5. The value 5 is an object of class int. The variable i refers to that object, so i gives you a way to work with an integer object. The assignment is therefore more than storing an unclassified piece of data: it gives a variable a value that belongs to a particular type.
Reading i = 5
Explain the class, object, and variable in the assignment i = 5.
- 1Identify the value
The value is 5.
- 2Identify the object
The value 5 is an object.
- 3Identify the class
The object 5 belongs to the int class.
- 4Identify the variable
The variable i refers to the integer object.
The assignment gives i a reference to an object of class int.
Methods and Fields
Objects combine data with behavior. Fields are variables bundled with a class. They store data associated with objects. Methods are functions bundled with a class. They define actions that objects can perform. Both are accessed through dotted notation: object.field refers to a field, while object.method refers to a method.
A useful mental test is to ask whether dotted notation identifies data or an action. A field exposes or stores associated data; a method performs an operation involving the object.
Lists as Built-In Objects
Lists make the class-and-object relationship visible. The list class defines behavior such as append, remove, and sort. It also provides fields for the items stored in a list object. When mylist = [1, 2, 3] is created, mylist is a list object containing the numbers 1, 2, and 3.
Following append
Explain what happens when mylist.append(4) is applied to a list that initially contains [1, 2, 3].
- 1Start with the object
The list object mylist contains [1, 2, 3].
- 2Find the method
The dotted expression mylist.append identifies the append method supplied by the list class.
- 3Perform the action
Calling append modifies the internal data field that stores the list items.
- 4Inspect the result
The list object now contains [1, 2, 3, 4].
The append method changes the state of mylist by modifying the field that stores its items.
| Part of a list object | Role | Source example |
|---|---|---|
| Stored items | Data held by the object | [1, 2, 3] |
| append | Action that adds an item | mylist.append(4) |
| remove | Action defined by the list class | remove |
| sort | Action defined by the list class | sort |
The list class combines stored data with methods that act on that data.
Built-In Types as Classes
The same model applies to built-in types. The value 5 is an object of the int class, and [1, 2, 3] is a list object created from the list class. The class supplies the organization for the object's data and behavior. This is why understanding classes and objects makes the behavior of integers and lists clearer.
When you later create your own custom classes, use this same separation: the class provides the blueprint, objects are the concrete instances, fields represent associated data, and methods represent actions that work with the objects.
Mistakes Beginners Make
Common mistake Treating a class and an object as the same thing.
What it looks likeCalling int and 5 interchangeable concepts.
Why it failsint is the class, while 5 is an object that belongs to that class.
Do this insteadDescribe the class as the blueprint and the object as the concrete instance.
Common mistake Thinking a variable is the object itself.
What it looks likeSaying that i is the integer object when discussing i = 5.
Why it failsThe value 5 is the object; i refers to that object.
Do this insteadSay that i refers to an object of class int.
Common mistake Confusing a method with the data it changes.
What it looks likeDescribing append as the list's stored items.
Why it failsappend is a method, so it represents an action. The list items are data stored by the list object.
Do this insteadSeparate the action, such as append, from the field containing the list's items.
Common mistake Ignoring the effect of a method call on object state.
What it looks likeExpecting mylist to remain [1, 2, 3] after mylist.append(4).Why it failsThe append method modifies the internal data storing the list items.
Do this insteadTrace the object's data before and after the method acts.
Check Your Model
Explain the relationship among the list class, the list object mylist, the stored items [1, 2, 3], and the append method in the example mylist = [1, 2, 3] followed by mylist.append(4).
Hints
- Identify which item is the class and which item is the object.
- Separate stored data from the action performed by append.
- Describe what changes after the method call.
Explain what the assignment i = 5 tells you about i, 5, and int. Include the words class, object, and refers to in your explanation.
Hints
- The value 5 is an object.
- int is the class associated with that object.
- The variable i refers to the object.
The Working Model
- 1A class is a blueprint or template, and an object is an instance created from that class.
- 2In i = 5, the value 5 is an object of class int, and i refers to that object.
- 3Fields are variables associated with objects and store data.
- 4Methods are functions associated with classes and define actions accessed through dotted notation.
- 5List objects combine stored items with methods such as append, remove, and sort; methods can change the object's state.
Key Takeaways
- A class defines a blueprint, while an object is a concrete instance of that class.
- Assignment creates or provides access to an object of a particular class; in i = 5, 5 is an int object.
- Fields represent data associated with objects, and methods represent actions objects can perform.
- Dotted notation accesses methods and fields through the object associated with a variable.
- Built-in types such as int and list use the same class-and-object model.