Start learning free

LESSON 177 OF 344 · OBJECT-ORIENTED PROGRAMMING

Creating Objects in Python: Calling a Class

Lesson Read the full explanation

TL ; DR:

  • An object is an instance of a class — a real, usable copy made from the blueprint.


  • In Python, you create one by calling the class name like a function (no new keyword):

    rex = Dog()
    

  • Each call creates a separate object, even if they all come from the same class.



Full lesson:

In the previous lesson you defined the Dog class — the blueprint. But a blueprint isn't a dog. To get an actual dog you can interact with, you need to create an instance of the class. An instance is also called an object.


Calling the class

Unlike most other OOP languages, Python doesn't use a new keyword. To create a new object, you simply call the class name like a function:

rex = Dog()

That's it — Python sees Dog() and makes a fresh instance.


Many objects from one class

You can call Dog() as many times as you want, and each call gives you a brand-new, independent object:

rex   = Dog()
buddy = Dog()
luna  = Dog()

rex, buddy, and luna are all Dog objects, but they're different objects. Changing one won't affect the others — they share the blueprint but live their own lives.


Your task

The Dog class is already defined for you. Below it, create three separate Dog objects named rex, buddy, and luna. The pre-filled print will confirm:

Created 3 Dog objects
STEP 1 OF 3
    Ctrl + Enter to check

    PYTHON · OBJECT-ORIENTED PROGRAMMING

    Creating Objects in Python, explained

    Classes and objects

    A class is a blueprint, and a blueprint is not a dog. To get something you can work with, you create an instance of the class. An instance is also called an object. The words are used almost interchangeably: Dog is the class, and each dog built from it is an object.

    Calling the class

    Many languages have a new keyword for this. Python does not. To create an object, call the class name the way you would call a function:

    class Dog:
        pass
    
    rex = Dog()
    print(rex)
    print(type(rex))
    

    The call Dog() builds a fresh instance and returns it, and the assignment stores it in rex. The second print shows its type, which is <class '__main__.Dog'> when the code runs as a script. The first print shows a default description that includes a memory address, so that line differs from run to run.

    Many objects from one class

    You can call the class as often as you like. Every call returns a new, independent object:

    rex = Dog()
    buddy = Dog()
    luna = Dog()
    
    print(rex is buddy)
    print(isinstance(luna, Dog))
    
    False
    True
    

    All three variables hold Dog objects, but they are three different objects. The operator is asks whether two names point to the very same object, and here the answer is no. They share the blueprint, but each lives its own life. Once attributes arrive in the next lessons, changing one dog will not change the others.

    It helps to keep the difference between a variable and an object clear. The variable rex is only a name. The object is what the name points to. Writing other = rex would not make a second dog: it would give the same dog a second name, and other is rex would be True.

    The lesson's exercise

    The Dog class is written for you, and so are three lines at the bottom that print a message for each variable that holds an object. The part left empty is the creation of the objects themselves. Without it, the first if rex is not None line fails with a NameError, because rex was never defined.

    1. Create rex. Write rex = Dog() below the class. The parentheses are required: rex = Dog without them would store the class itself, not an object.
    2. Create buddy. Write buddy = Dog() on the next line. This is a second call, so it builds a second object. It is not a copy of rex and does not depend on it.
    3. Create luna. Write luna = Dog(). One class can produce as many objects as you want.

    With the three lines in place, each of the pre-filled checks passes, and the program prints one line per variable:

    rex exists
    buddy exists
    luna exists
    

    Common mistakes

    • Leaving out the parentheses. rex = Dog gives the variable a second name for the class. No object is made, and later code that expects a dog behaves oddly.
    • Using new. rex = new Dog() is a syntax error in Python.
    • Expecting one call to make several objects. a = b = Dog() creates one object with two names. For three dogs you need three calls.
    • Passing arguments the class does not accept. The current Dog takes none. Dog(3) raises a TypeError that says Dog() takes no arguments. Constructors, covered in a later lesson, are how a class accepts arguments.

    Key takeaways

    • An object, or instance, is a usable thing built from a class.
    • Python creates one by calling the class: Dog(). There is no new keyword.
    • Every call makes a separate object, and is can confirm they are different.
    • A variable is only a name for an object, and two names can refer to one object.
    Keep going with PythonAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free