Concepts / Creating Objects from Classes

Creating Objects from Classes

A class is a template that defines what data and code each object will have; the class keyword does not create an object.

  • Programming
Interactive lab

Try it: Classes, Instances and Attributes

How a class creates instances with __init__ and self, why an attribute read checks the instance's __dict__ before the class, why assigning through an instance never changes the class, how a class-level list is shared by every instance, and why obj.method(x) is Class.method(obj, x).

How it works

  1. Dog(...) makes a new, empty instance and calls Dog.__init__(new_object, ...); self.name = ... stores into that object's __dict__.
  2. Reading obj.attr looks in obj.__dict__ first, then in the class.
  3. Assigning obj.attr = value always writes obj.__dict__, so it shadows a class attribute for that one object only.
  4. Mutating a class-level list (self.tricks.append) changes the one list every instance shares.
  5. obj.method is found on the class and bound to obj, so obj.method(x) runs Class.method(obj, x).

Default run (30 steps): Ready: class Dog and the statements you picked. Watch where each attribute is found and where each assignment writes. … Finished. Printed: ['sit'] / Wolf / Canis familiaris / Fido is 5.

Simplified: The lab's own small Python interpreter runs a fixed, hand-written script (you choose the variant and the values, never the code); nothing is executed as real Python. Every script was run in CPython 3.12 with the same inputs and the lab reproduces its output and error messages. Object numbers are illustrative, not real id() values.

Educational simulation

Loading the simulation…

A Template Is Not an Object

When you first see the class keyword, it is easy to think that writing a class has already created an object. It has not. A class is a template that defines the data and code objects of that type will have. An object is an individual instance created from that template.

In the real world

A cookie cutter provides a useful analogy. The cutter is the template, while each cookie made with it is a separate object. In Python, the class is the template and each object created from it is an instance with its own data.

PartyAnimal classPartyAnimal objectcalled to create
What changes when a class is defined and then called to create an object?

What Happens During Creation

Object creation begins when you call the class as if it were a function. For example, an = PartyAnimal() creates a new PartyAnimal object. As part of that creation, Python automatically runs the object's __init__ method. That method sets up the object's initial attributes.

python
Output
After an = PartyAnimal(), the object has an.x set to 0.
PartyAnimal()new object__init__x = 0createsruns automaticallysets
What happens to an object's data when the class is called?

How self Selects the Object

Definition

The self parameter in a method refers to the specific object instance on which that method was called. It gives the method access to that object's attributes, so the method can read or modify the correct object's data.

Code walkthrough
class PartyAnimal:
    def __init__(self):
        self.x = 0

    def party(self):
        self.x = self.x + 1
        print(self.x)

an = PartyAnimal()
an.party()
an.party()
an.party()
  1. 1
    The class defines a party method that changes an object's x attribute.
  2. 2
    an = PartyAnimal() creates an object and initializes its x attribute to 0.
  3. 3
    The first an.party() call changes an.x from 0 to 1 and prints 1.
  4. 4
    The second call changes an.x from 1 to 2 and prints 2.
  5. 5
    The third call changes an.x from 2 to 3 and prints 3.
  • self.x = 0 gives the new object an initial x attribute.
  • self.x = self.x + 1 reads the object's current x value and stores the increased value.
  • When an.party() is called, self refers to an.
Output
1
2
3
anPartyAnimal objectparty()self refers to anxvalue changescallsreads and modifies
How does a method call reach the specific object's attributes?

A Complete Class Walkthrough

Worked example

Tracing Three Method Calls

Create a PartyAnimal object, initialize its counter, and call its party method three times.

  1. 1
    Define the class

    The class definition describes the data and behavior that PartyAnimal objects will have. Defining it does not create a PartyAnimal object.

  2. 2
    Create the object

    The expression an = PartyAnimal() calls the class. Python creates an object and automatically runs __init__ for that object.

  3. 3
    Initialize the attribute

    The __init__ method assigns 0 to self.x. For this call, self refers to an, so the object's x value starts at 0.

  4. 4
    Call party three times

    Each an.party() call runs with self referring to an. The method increases the same object's x value by 1 on each call.

Result

After the three calls, an.x is 3.

python
Output
1
2
3

The important sequence is class definition, object creation, initialization, and method calls. The class supplies the structure and behavior. The object carries its own current attribute value. The method uses self to update that object's state.

Mistakes Beginners Make

  • Common mistake

    Assuming that the class keyword creates an object.

    What it looks like
    class PartyAnimal:
    Why it fails

    This defines the template and its behavior, but it does not create an individual PartyAnimal object.

    Do this instead

    Call the class, as in an = PartyAnimal(), to create an object.

  • Common mistake

    Treating __init__ as an ordinary method that must be called manually after object creation.

    What it looks like
    an = PartyAnimal()
    Why it fails

    The __init__ method runs automatically when the object is created and sets up its initial attributes.

    Do this instead

    Put initial attribute assignments in __init__ and allow object creation to trigger it.

  • Common mistake

    Leaving self out of an instance method's access to object data.

    What it looks like
    x = x + 1
    Why it fails

    The method needs self to refer to the specific object's attribute.

    Do this instead

    Use self.x when reading or modifying the object's x attribute.

  • Common mistake

    Thinking that one method call changes every object created from the class.

    What it looks like
    an.party()
    Why it fails

    self refers to the particular object on which the method was called.

    Do this instead

    Track the object before the dot. In an.party(), self refers to an.

Practice the Creation Sequence

EASY

Write a class named Counter with an __init__ method that gives self.value an initial value of 0. Add a method named increase that adds 1 to self.value. Then create an object named counter and call increase twice. Before checking your work, state what value counter.value should have after the two calls.

Hints
  • Define the class before creating the object.
  • Initialize the attribute with self.value inside __init__.
  • Use self.value both when reading the current value and when storing the increased value.

What do you think happens?

If an object starts with x equal to 0 and its method increases self.x by 1, what is x after three method calls?

  • 0

  • 1

  • 2

  • 3

Reveal answer
Answer:

3

The first call changes the value to 1, the second to 2, and the third to 3. Each call uses self to modify the same object's attribute.

The Object-Oriented Pattern

  1. 1
    A class is a template that defines the data and code objects will have; defining the class does not create an object. Calling the class creates an object. The __init__ method runs automatically during creation and establishes the object's initial attributes. Methods use self to refer to the specific object that called them. Because each object has its own attributes, one class can create multiple objects with independent data.

Key Takeaways

  • A class is a template, while an object is an individual instance created from that template.
  • The class keyword defines structure and behavior; it does not create an object by itself.
  • Calling a class creates an object and automatically runs its __init__ method.
  • The __init__ method establishes initial attributes such as self.x = 0.
  • The self parameter connects a method to the particular object's attributes that the method reads or changes.

More concepts starting with C