Python Class Tutorial
You’ve probably heard many times that Python is an object-oriented programming language. What does this mean for you as a programmer who must get a job done? It can mean a lot of time saved as you use code once for a variety of situations. In this Python class tutorial we’ll see how this can work in a few situations. You might want to take a course to get more background on object-oriented programming.
Defining a Class With the __init__ Method
Suppose you are the manger of an aviary and you need to display information about all of the birds in your collection. The information will be available to the visitors to the aviary. There are many characteristics common to all birds. There is no need to repeat these for each and every bird in the collection. A class in object oriented programming is a blueprint describing the set of characteristics and behaviors a member of the class can have. You can review other Python features with this course to help with the examples. This article on Python projects will also get you started to apply these ideas.
In Python it might look something like this:
1 Class Bird:
2 def __init__(self, kind, food, home)
3 self.kind = kind
4 self.food = food
5 self.home = home
6 def tell_info(self)
7 print “I am a %s, I eat %s, and I come from %s” % (self.kind, self.food, self.home)
We have outlined a definition of a bird. For each bird, we know what kind of bird it is, what it eats, and where it comes from. In line 1, we define the name of the class. Line 2, the init method, is known in object-oriented programming as a constructor. This tells the program to expect a set of attributes and the order in which they will be given. The first item in this list, self, is a place holder and will become clearer in a bit. Lines 3-5 define the actual variables that will be used for these attributes. These will all have specific values for each specific bird in the collection.
In lines 6 and 7 we have given each bird a behavior. It will tell you about itself. Perhaps the code is for an app where the visitor clicks on an image of the bird when they are in front of the cage. If this gets you thinking about writing an app, try a course on writing GUI applications in Python.
The method tell_info prints on the screen the information about that particular bird. A method is essentially a function that members of the class are able to perform. The %s is a placeholder in the output string, and the list in parentheses gives the variables that should fill in each placeholder, in order. Now our class has several attributes and one behavior.
Now that the class is defined, creating each individual is easy. But let’s think about this for a moment. You have several kinds of bird, and you may have several specimens of each kind. So we need to have a class for each kind of bird. Here is where the power of classes becomes evident.
8 Class Penguin (Bird):
9 def __init__(self):
10 Bird.__init__(self, “penguin”, “fish”, “Antarctica”)
11 Class Hawk (Bird):
12 def __init__(self):
13 Bird.__init__(self, “hawk”, “rabbits”, “North America”)
In lines 8 and 11 we have defined 2 specific birds. The entry (Bird) tells the program that the Penguin and the Hawk will inherit the attributes and behaviors of the class Bird. They are child classes of the parent class bird. Inheritance is a powerful feature of classes and object-oriented programming. Any time a new type of bird is added to the collection, all that is needed is a few lines of new code to define that new type as a child class of Bird. It automatically takes on all the the functioning of the Bird class.
If we want the penguin to tell us about itself, we program the command
and we get the output
I am a penguin, I eat fish, and I come from Antarctica
This is where the self entry in the definitions comes into play. When this is run, the definition of the Bird class will be accessed, but everywhere the word “self” appears it will be replaced by “penguin” and all of the information specific to the penguin will be used. In object-oriented programming, Penguin and Hawk are said to be specific instances of the class Bird. The self placeholder essentially tells the program, “use the current instance of this class to perform this method.” The word “self” is not really a reserved word. Any word could be used here, but “self” is used often by convention and it makes your code easier to read and debug later.
Defining a Class Without __init__
Let’s look at another example, and see another way to initialize a class. Suppose we are programming a game with a town full of people. We want to keep track of certain facts about each person.
name = “name not defined”
age = 0
occupation = “occupation not defined”
def setNameandAge(self, x,y):
self.name = x
self.age = y
def setOccupation(self, x):
self.occupation = x
print ” Hi. My name is %s and I am %s.” %(self.name, self.age)
print (“I am a “, self.occupation, “.”)
In this case we have listed attributes with an initial value and then we defined methods for the class that let us set values for the attributes later.
When we want to have actual instances of citizens, we define them and set their attributes.
p1 = Citizen
Here, we are telling the program that p1 is a specific instance of the class Citizen, and we go on to set the attributes. The attribute methods are called with the dot notation, meaning “for p1, perform the method and use the following list as values for this instance.” If we wanted Sarah to introduce herself we would type:
and we would get the reply
Hi. My name is Sarah and I am 24.
I am a doctor.
These examples should get you started with classes in Python. You can continue your learning with Python, the Next Level.
Top courses in Python
Python students also learn
Empower your team. Lead the industry.
Get a subscription to a library of online courses and digital learning tools for your organization with Udemy for Business.