Concepts / Creating Custom Classes

Creating Custom Classes

A class is a blueprint; an object is an instance of that blueprint. When you assign a value like i = 5, you create an object of class int.

  • Programming

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.

createscreatesintclass5objectanother integerobject
How does one class provide a common blueprint for multiple objects?

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.

namesrefers toinstance ofi = 5assignmentivariableintclass5int object
What object is created when i = 5 runs, and how is the variable connected to it?

Reading i = 5

Explain the class, object, and variable in the assignment i = 5.

Identify the value: The value is 5.

Identify the object: The value 5 is an object.

Identify the class: The object 5 belongs to the int class.

Identify 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.

containsdefinesfieldstores dataobject dataobject.fieldmethodperforms an actionobject actionobject.method
What is the difference between data held by an object and an action performed on that object's data?

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].

Start with the object: The list object mylist contains [1, 2, 3].

Find the method: The dotted expression mylist.append identifies the append method supplied by the list class.

Perform the action: Calling append modifies the internal data field that stores the list items.

Inspect 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.

callschanges tomylist[1, 2, 3]appendmethodmylist[1, 2, 3, 4]
What changes when the append method acts on a list object's stored items?
Part of a list objectRoleSource example
Stored itemsData held by the object[1, 2, 3]
appendAction that adds an itemmylist.append(4)
removeAction defined by the list classremove
sortAction defined by the list classsort

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.

includesincludesinstance ofinstance ofbuilt-in classesintclass5objectlistclass[1, 2, 3]object
How do familiar Python values relate to their built-in classes?

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

  • Treating a class and an object as the same thing.

    int is the class, while 5 is an object that belongs to that class.

    Fix: Describe the class as the blueprint and the object as the concrete instance.

  • Thinking a variable is the object itself.

    The value 5 is the object; i refers to that object.

    Fix: Say that i refers to an object of class int.

  • Confusing a method with the data it changes.

    append is a method, so it represents an action. The list items are data stored by the list object.

    Fix: Separate the action, such as append, from the field containing the list's items.

  • Ignoring the effect of a method call on object state.

    The append method modifies the internal data storing the list items.

    Fix: Trace the object's data before and after the method acts.

Check Your Model

MEDIUM

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.
EASY

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

  1. A class is a blueprint or template, and an object is an instance created from that class.
  2. In i = 5, the value 5 is an object of class int, and i refers to that object.
  3. Fields are variables associated with objects and store data.
  4. Methods are functions associated with classes and define actions accessed through dotted notation.
  5. List 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.