Section 2.1 Classes and Objects
Depending on how deep your knowledge of Python and programming in general is, you may or may not be familiar with classes and objects. These two important
Object-Oriented Programming (
OOP) concepts will briefly be discussed. If you already have a good understanding of classes and objects in Python, this section may be skipped.
Objects in the context of programming are instances of classes. Objects contain
attributes (also referred to as
instance variables), which are data that describe the object or are associated with the object, and
methods, which are special functions used by the object. Methods are typically actions the object can perform, or can be used to make changes to the objectβs attributes.
Classes can be thought of as being similar to blueprints or a recipe; they hold details of how to create an instance of an object. Classes contain a special method called a
constructor that is used to create an instance of an object. Once the object is created, it will use the class definition to define its attributes and call methods.
The best way to understand classes and objects is to see them in action. Letβs define a
Dog class in Python:
class Dog:
def __init__(self, name, breed, fur_color):
self.name = name
self.breed = breed
self.fur_color = fur_color
self.trained = False
print("Dog named " + self.name + " created!")
def bark(self):
print(self.name + " says woof!")
def sit(self):
if self.trained:
print(self.name + " sits.")
else:
print(self.name + " has not been trained.")
def train(self):
self.trained = True
Letβs unpack what is going on in this code. The first line is where we declare the class definition and name it
Dog. Next, we have a special method called
__init__. This
__init__ method is the constructor and is required for every Python class definition. Within the
__init__ method, attributes are defined. As you can see, the attributes
name,
breed, and
fur_color must be defined when creating a Dog object using this class definition, but the
trained attribute is defined within the constructor and is initialized as
False. We can also have the
__init__ method run any code, such as the print statement informing us that a Dog object was created.
The next three blocks of code are the classβs methods. These include
bark(self),
sit(self), and
train(self). As you can see, the class defines attributes (the variables in the
__init__ method) and methods for instances of the Dog class.
Within each method, and for each attribute, you will notice the use of
self. This is required in Python.
self simply indicates that an attribute or method is being used for a specific instance of an object created with a class.
Next, we will use this class to create a new
Dog object. We will call this new Dog object
my_dog:
In the final line of code, we have created an object called
my_dog. We have initialized its attributes, setting
name to Rex,
breed to pug, and
fur_color to brown.
Now that we have created a
Dog object using the class we defined, we can utilize the classβs methods:
Note 2.1.1.
When running the code above, the line
Rex has not ben trained. will appear in the output when calling the
sit() method. Try adding a one or more lines of code so that
Rex sits. appears in the output!
Now, we have a full class definition and have utilized its methods. Class definitions in Java will be covered thoroughly in chapter 6. For now, it is important to know that Python programs can be written without using classes at all. Java, on the other hand, requires all code to reside in a class. This will be discussed in the next section.
You have attempted
of
activities on this page.