Attributes and Methods
Remember, that you must refer to the variables and methods of the same object using the self only . This is called an attribute reference .
One Blueprint, Several Objects
Imagine a class as a reusable blueprint. The blueprint describes a kind of object, while each object created from that blueprint is a concrete instance. One class definition can therefore be used to create multiple objects. Those objects share the same class definition, but each object is independent and has its own memory location.
The Smallest Class Definition
The simplest possible class requires four parts: the class keyword, a class name, a colon, and the pass statement. The pass statement gives the class body a valid statement even though the class does not yet contain attributes or methods.
This definition creates the class named Person. At this point, the class is the reusable blueprint. It is not yet a particular Person object.
From Class to Instance
Objects are created by calling the class name as if it were a function. Each call creates an object from the class definition. Calling the same class more than once creates multiple objects, and each object is independent even though all of them come from the same class.
Creating Two Person Objects
Use the Person class definition to create two separate objects.
Define the class: The class Person is the reusable blueprint.
Call the class once: person_a = Person() creates one object instance from the Person class.
Call the class again: person_b = Person() creates another object instance. It comes from the same class definition but is independent of person_a.
The single Person class definition has produced two separate object instances.
self and Attribute References
When a class has variables or methods that belong to an object, refer to those variables and methods through self. Referring to the same object's variables and methods using self is called an attribute reference. The important connection is that self identifies the particular object whose variables or methods are being referred to.
Mistakes Beginners Make
Treating the class as though it were already one particular object
A class is the reusable blueprint; an object is the concrete instance created from that blueprint.
Fix:
Use the class name to create objects, then think of each created object as a separate instance.Leaving out pass in the simplest empty class
The simplest class requires a valid class body, and the source identifies pass as the required statement for the empty class.
Fix:
Write class Person: followed by an indented pass statement.Referring to an object's variables or methods without self
Variables and methods of the same object must be referred to using self. This is called an attribute reference.
Fix:
Use self when referring to variables and methods belonging to the same object.Assuming two objects from one class are the same object
Multiple objects can come from one class definition, but each object is independent and gets its own memory location.
Fix:
Treat each call to the class name as creating another independent object instance.
Practice the Distinction
Write the simplest possible class named Book. Then write two statements that create two separate object instances from it. Finally, label which name is the class and which names refer to the objects.
Hints
- The empty class needs class, a name, a colon, and pass.
- Create an object by calling the class name.
- Use two different names for the two object instances.
What do you think happens?
If Book() is called twice, how many object instances are created?
Reveal answer
Answer: Two independent object instances
Objects are created by calling the class name, and multiple objects can be created from one class definition. Each object is independent and gets its own memory location.
Key Takeaways
- A class is a reusable blueprint, while an object is a concrete instance created from that blueprint.
- The simplest class uses the class keyword, a name, a colon, and pass.
- Calling a class name creates an object instance.
- One class can create multiple independent objects, each with its own memory location.
- Use self to refer to the variables and methods belonging to the same object; this is an attribute reference.
Key Takeaways
- A class provides a reusable blueprint for objects.
- An object is an individual instance created by calling a class name.
- The simplest empty class contains class, a name, a colon, and pass.
- Multiple independent objects can be created from one class definition.
- self refers to the particular object whose variables and methods are being accessed.