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.
- Create
rex. Writerex = Dog()below the class. The parentheses are required:rex = Dogwithout them would store the class itself, not an object. - Create
buddy. Writebuddy = Dog()on the next line. This is a second call, so it builds a second object. It is not a copy ofrexand does not depend on it. - Create
luna. Writeluna = 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 = Doggives 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
Dogtakes none.Dog(3)raises aTypeErrorthat saysDog() 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 nonewkeyword. - Every call makes a separate object, and
iscan confirm they are different. - A variable is only a name for an object, and two names can refer to one object.