. Advertisement .
..3..
. Advertisement .
..4..
Private methods in Python have the same function as the wires in your lighting system. While regular users can interact with the light bulb and the light switch with no problem, the wires are a different story. Nevertheless, it’s responsible for the smooth operation.
That is why it’s vital for you to grasp the way to manipulate private methods in Python.
Private Methods In Python
Private Access Modifier
Before we can delve deep into this section, you must first understand more about private methods in Python. In short, the private keyword refers to an access modifier type that most object-oriented programming languages use.
Its main job is to limit a variable or function’s visibility, as declaring them private places a limit that only allows access to the encapsulating class.
__init__()
One of the most known Python private methods is __init__()
, which we mostly use in the class constructor role for all types of class objects. We usually call it when either a class or an object gets instantiated. Of course, it also depends on the arguments of the method.
Let’s take a small example with __init__()
declaring a Student class having two fields:
class Student:
studentName = ‘’
gpa = 0
def __init__(self, n, g):
self.studentName = n
self.gpa = g
With this, the class has been built, and you can no longer access its content. To do so, you have to create an instance and then call the constructor.
sys.path.append(".")
from studentClass import Student
student = Student("Mike Chen", 80)
print(student.studentName, student.gpa)
There are other ways to call the __init__()
method. For example, you can first instantiate the class, put it into a new variable, and then call it explicitly.
An easier approach is to call it from the class straight away. You do need to put the self parameter before, though. Some people refrain from using this approach due to this reason.
How To Declare Private Methods in Python
The most vital thing to remember is that you must always put a double underscores prefix before the method you want to declare. Forget to do so, and it will become just another default public one.
Let’s take our previous example and make a subclass named HonorStudent. Its constructor is based on the original Student class.
class Student:
studentName = ‘’
gpa = 0
def __init__(self, n, g):
self.studentName = n
self.gpa = g
def __study(self)
print("Studying")
class HonorStudent(Student)
occupation = “Student Council”
scholarship = 0
def __init__(self, studentName, gpa, occupation, scholarship):
Student.__init__(self, studentName, gpa,)
self.occupation = occupation
self.scholarship = scholarship
def study(self)
print("Honor student is studying")
self.study()
Once you master this approach, challenges like importing Excel files become much more manageable.
Conclusion
Knowing how to approach private methods in Python is one of the most vital core skills that a programmer can possess. It allows you to perform some very interesting manipulation techniques, which high-level projects require on a regular basis.
If you find this article helpful, look forward to our next publication.
Leave a comment