Creating Classes and Objects
A class defines a type of object and lets us create separate objects with the same structure. In Figure 12, MyClass is the class, while p1, p2 and p3 are names referring to objects created from it.
UML means Unified Modeling Language, a shared set of symbols for describing software. You can read these small diagrams before learning to draw them. A class box names a type; an object box names a particular instance of that type. In the underlined label p1 : MyClass, p1 names the object and MyClass names its class. The underline distinguishes this object label from the plain class name. Here, one class definition can produce several separate instances. The later UML lessons introduce each diagram type in more detail.
Define a Class
To create a class, use the keyword class, followed by a name and a colon (:). Write the class body on the following lines, using indentation. The body must contain a statement, so an empty class uses pass as a placeholder. This lets us define MyClass before adding any data or actions:
class MyClass:
passpass performs no action. The class definitions in this section produce no visible output.
A class can have attributes: named values attached to the class or its objects. To give MyClass a value, replace pass with the assignment x = 5.
class MyClass:
x = 5This defines x as a class attribute of MyClass. We will explore attributes in the next section.
Create Objects
Once a class is defined, it can be used to create objects, also called instances of the class.
p1 = MyClass()
print(p1.x)Output
5
In this example, the parentheses in MyClass() call the class to create an instance. The assignment gives it the variable name p1. The dot in p1.x accesses x, which the object finds on its class; print(p1.x) displays 5.
Predict what the following code will print, then run it:
p1 = MyClass()
p2 = MyClass()
p3 = MyClass()
print(p1.x)
print(p2.x)
print(p3.x)Check your prediction
Output
5
5
5
Each call to MyClass() creates a separate instance. All three objects find the same value of x on their class. Sharing that value does not make them the same object.
Aliasing: Two Names for One Object
Two variable names can refer to the same object. This is called aliasing. In the following example, alias = p1 gives the object a second name, alias; it does not copy the object. The statement del p1 then removes the name p1.
Continue with the p1 object created above. The final part of this example deliberately uses p1 after removing it. The try block attempts that access; Python raises NameError because the name is no longer defined. The except NameError block handles the error and prints a message.
alias = p1
del p1
print(alias.x)
try:
print(p1.x)
except NameError:
print("NameError: p1 is no longer defined")Output
5
NameError: p1 is no longer defined
The first line comes from print(alias.x): the object is still available through alias, so its value can still be read. The second line is the message printed by the error handler. Removing one name did not remove the object.
Try it: in the class definition above, change x = 5 to x = 8 and run that class definition again. Before rerunning the example that creates three objects, predict the three printed values. Then run it to create three new objects and check your prediction. Explain why changing the class affects all three results.
We can now create objects from a class and access a class attribute through them. The next section develops this idea with data stored on individual objects and methods that work with that data.