Concepts / Creating Custom Classes

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.

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

intclass5objectanother integerobjectcreatescreates
How does one class provide a common blueprint for multiple objects?
Key point

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.

i = 5assignmentivariableintclass5int objectnamesrefers toinstance of
What object is created when i = 5 runs, and how is the variable connected to it?
Worked example

Reading i = 5

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

  1. 1
    Identify the value

    The value is 5.

  2. 2
    Identify the object

    The value 5 is an object.

  3. 3
    Identify the class

    The object 5 belongs to the int class.

  4. 4
    Identify the variable

    The variable i refers to the integer object.

Result

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.

fieldstores dataobject dataobject.fieldmethodperforms an actionobject actionobject.methodcontainsdefines
What is the difference between data held by an object and an action performed on that object's data?
Key point

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.

Worked example

Following append

Explain what happens when mylist.append(4) is applied to a list that initially contains [1, 2, 3].

  1. 1
    Start with the object

    The list object mylist contains [1, 2, 3].

  2. 2
    Find the method

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

  3. 3
    Perform the action

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

  4. 4
    Inspect the result

    The list object now contains [1, 2, 3, 4].

Result

The append method changes the state of mylist by modifying the field that stores its items.

mylist[1, 2, 3]appendmethodmylist[1, 2, 3, 4]callschanges to
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

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.

built-in classesintclass5objectlistclass[1, 2, 3]objectincludesincludesinstance ofinstance of
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

  • Common mistake

    Treating a class and an object as the same thing.

    What it looks like

    Calling int and 5 interchangeable concepts.

    Why it fails

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

    Do this instead

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

  • Common mistake

    Thinking a variable is the object itself.

    What it looks like

    Saying that i is the integer object when discussing i = 5.

    Why it fails

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

    Do this instead

    Say that i refers to an object of class int.

  • Common mistake

    Confusing a method with the data it changes.

    What it looks like

    Describing append as the list's stored items.

    Why it fails

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

    Do this instead

    Separate 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 like
    Expecting mylist to remain [1, 2, 3] after mylist.append(4).
    Why it fails

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

    Do this instead

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

Related concepts

More concepts starting with C