Welcome to another tutorial, here you will learn about Class in Python.
You already learned that classes are a collection of variables, and functions bounded together.
In other to define a class, we simply have to use the class keyword followed by the name that we want to use, then a colon symbol ‘:’. However, this is the standard method to start the naming of a class with a capital letter, followed by the camel case style.
Check out the example below:
class MyClass:
# member variables
variable1 = something
variable2 = something
# member functions
def function1(self, parameter1, ...):
self.variable1 = something else
# defining a new variable
self.variable3 = something
function1 statements...
def function2(self, parameter1, ...):
self.variable2 = something else
function2 statements...
For a better understanding of the code above, we used the keyword class to instruct the compiler that from here new class definitions start, and the name of the class, which is my class.
Notice that, in Line 2, a comment was added, while in lines 3 and 4, two variables (variable1 and variable2) were declared and they were also initialized with some values.
Also, in the example, there are two functions defined further below the lines, aimed at explaining the syntax. You will also notice that we used the def keyword, except for a new parameter name self. The function self is a mandatory parameter for every function defined in a class, it represents the currently active object of the class.
A class can be seen as a blueprint or a template, and storage space is not assigned when a class is defined. While objects are instances of a class, that holds the data variables declared in the class and the member functions work on these class objects. Check out the example below:
myObject = MyClass()
where, MyClass is the name of the class and myObject is the object variable
From the example above, we specified the object's name and we called a function that had the same name as the class to which it belongs. To know how to initialize a list we have to write myList = list().
The examples below will help us to understand all that we have learned in this tutorial.
In this first example, we will define a class with one variable and two methods; we will also create an object of that class and will try to access the variables and the member functions too.
class Apollo:
# define a variable
destination = "moon";
# definig the member functions
def fly(self):
print ("Spaceship flying...");
def get_destination(self):
print ("Destination is: " + self.destination)
From the above example, a class named Apollo was defined. Also, in the function get the destination, we wrote self.destination, as this will give the value stored in the variable destination for the object for which the function get destination will be called.
Now, we will create two objects here, for this class.
class Apollo:
# define a variable
destination = "moon"
# defining the member functions
def fly(self):
print ("Spaceship flying...");
def get_destination(self):
print ("Destination is: " + self.destination);
# 1st object
objFirst = Apollo()
# 2nd object
objSecond = Apollo()
At this point, two objects have been defined in our class, and the objects have a variable destination in them, which is assigned the value ”moon”
Now, in other to access any member variable or a member function using the object. The dot ‘.’ Is used. Check out the example below:
class Apollo:
# define a variable
destination = "moon"
# defining the member functions
def fly(self):
print ("Spaceship flying...");
def get_destination(self):
print ("Destination is: " + self.destination);
# 1st object
objFirst = Apollo();
# 2nd object
objSecond = Apollo();
# lets change the destination for objFirst to mars
objFirst.destination = "mars";
From the example above, the variable for the variable destination was updated to “mars” for the object objFirst.
We can now call the member functions
class Apollo:
# define a variable
destination = "moon"
# defining the member functions
def fly(self):
print ("Spaceship flying...");
def get_destination(self):
print ("Destination is: " + self.destination);
# 1st object
objFirst = Apollo();
# 2nd object
objSecond = Apollo();
# lets change the destination for objFirst to mars
objFirst.destination = "mars";
# objFirst calling fly function
objFirst.fly();
# objFirst calling get_destination function
objFirst.get_destination();
# objSecond calling fly function
objSecond.fly();
# objSecond calling get_destination function
objSecond.get_destination();