So, What Is A Python Class? In the Python programming language, each class bundles or groups data and functionality together. Another word for this is ENCAPSULATION. All ‘things’ that belong to that class have access to the same data and functionality specific to that class.
Using the above screenshot 👆 as a guide, we will explain a Python class line by line:
1.) CLASS NAME: This is the name that you (the programmer) give the class. In this case, the class name is Vertebrates. It has to be unique and ideally something that describes the purpose of the class. It can be words and numbers or a combination of both but it cannot be reserved words used by the python language like class or def. This entire block of code (the indented portion) is called a class declaration.
2.) DOCSTRING: A docstring is the first statement of a class or function that serves as a simple explanation of what the class or function does. We normally use “triple, double quotes” to enclose the docstring. Find out more HERE. In this case, our docstring is “””All animals with a backbone””” but it can be any short statement enclosed in triple, double quotes. We can print the contents of docstring to the screen with the special attribute __doc__:
print(Vertebrates.__doc__) #print docstring to screen
#outputs: All animals with a backbone
3.) CLASS VARIABLE: This a variable that is defined in a class. All instances created that belong to this class have access to the data in this class variable. In this case, the class variable is set to True but it can have any data type. You can also have more than one; as many as you wish in fact.
4.) SPECIAL METHOD: __init__() is a special method that is built into Python. Whenever we create a class object, this special method is called automatically. In this case, it sets the instance variable to the value of the classification parameter. When a class is first created it is empty, this special function can be used to set initial values for instance variables and more. There are other built in special methods besides __init__().
The reserved word self is used to represent the current instance of the class. It does not have to be called self, it could be called anything BUT it must be the FIRST parameter of any method defined in the class. You should continue to call it self for convention sake and readability in your code.
5.) INSTANCE VARIABLE: Instance variables refer to data attributes unique to each instance as opposed to class attributes which are shared by all instance of a class. In this case our instance variable is c but we refer to it here as self.c so that we know we are referring to the current instance and not any other.
6.) USER DEFINED METHOD: Unlike the built-in special methods, this method is defined by the programmer. A method is defined in a similar way to a function with the def reserved word, the only difference is that it belongs to a class. Our method is called getSubClassCount and we must provide self as the first parameter at all times so that the class knows that the method is a part of the class.
7.) CLASS OBJECT DECLARATION: When the class definition executes in python code, a class object is created. We created a class object or simply an object of class Vertebrates by using the class name in the form Vertebrates(5). Vertebrates() returns an instance of the class which it assigns to our class object v. Python requires us to use function notation in a class object declaration. In this case, because our __init__() is expecting a parameter called classification, we must supply it to the class at the point of our class object declaration. If we don’t supply this argument, the program will give us the following error: TypeError: init() missing 1 required positional argument: ‘classifications’.
We can actually declare multiple class objects for the same class, each specific class object would be called an instance of the Vertebrate class:
v = Vertebrates(5)
v1 = Vertebrates(5)
v2 = Vertebrates(5)
Once we define our class and declare an instance of our class object, we can now use our class:
v.getSubClassCount()
#outputs: There are 5 sub classes of vertebrates.
Using the class object name followed by the . operator and the name of the method, we can access the methods in our class that do the actual work.
Here is the FULL code:
class Vertebrates: #class name
"""All animals with a backbone""" #docstring
backbone = True #class variable
def __init__(self, classifications): #special method
self.c = classifications #instance variable
def getSubClassCount(self): #user defined method
print(f"There are {self.c} sub classes of vertebrates.")
print(Vertebrates.__doc__) #special __doc__ attribute
v = Vertebrates(5) #class object declaration, one instance of class
v1 = Vertebrates(5) #class object declaration, 2nd instance of class
v2 = Vertebrates(5) #class object declaration, 3rd instance of class
v.getSubClassCount() #class method usage
#Outputs:
#All animals with a backbone
#There are 5 sub classes of vertebrates.
CONCLUSION
No discussion on classes is complete without also mentioning OOP or Object Oriented Programming. OOP is a concept developed in the 1960s by American Alan Kay and it is a conceptual approach to software development. The Python Programming language supports OOP but it also supports other programming paradigms as well.
HERE is another great article that goes deeper into Python Classes. Good luck! 👌👌👌