Menu Close

What are Python Access modifiers?

In this guide, we will see what is Python access modifiers and also we will learn types of access modifiers and how to implement them in the python program using appropriate examples.

What is Python Access modifiers?

In various object-oriented programming language access modifiers is used to restrict the access of variable and properties of the class.

Most programming languages have three types of access modifiers, That is Public, Private, and Protected, In Python, they are also the same. but In Python, the declaration of access modifiers is different rather than other languages like java, c++, etc.

Why should use Access modifiers in Python?

Python access modifiers are used to control the access of data members and member functions of the class.
Access modifiers in Python play an important role to secure the data and restrict to access data from unauthorized access.

Types of Access modifiers in Python.

In Python, There are three types of Python Access Modifiers available.

  • Public Access Modifier
  • Private Access Modifier
  • Protected Access Modifier

Let’s discuss all the access modifiers in python step by step.

Public Access Modifiers

The members of the class that is declared public are easily accessible by any part of the program but within the program. By default, all the data members and members function in class declared public.

Example:

class Student:
	def __init__(self, name, age):
		self.studentName = name
		self.studentAge = age
	def printNow(self):
		print(f"My name is {self.studentName} and my age is {self.studentAge}")

#Create object of Student class					
s1 = Student('John', 34)
s1.printNow()

Output

My name is John and my age is 34

In the above program studentName and studentAge are public data members and printNow() is the public member function of the class Student.

Protected Access Modifiers

The members of class that are declared as protected, are accessible by inside the class as well as inherited class. To declare protected data member within class, we need to use single underscore (_) before attribute.

Example:

class Student:	
	_rollNo = 34
	def __init__(self, name, age):
		self.studentName = name
		self.studentAge = age
	def _printNow(self):
		print(f"My name is {self.studentName} and my age is {self.studentAge}")
				
class Programming(Student):	
	def __init__(self, name, age):
		super().__init__(name, age)
		
	def displayDetails(self):
		self._printNow()
		print("Roll No is:- ", self._rollNo)
					
s1 = Programming('John', 45)
s1.displayDetails()

Output

My name is John and my age is 45
Roll No is:-  34

In above program rollNo is the Protected data member and _printNow() is the Protected member function of class which are access in child class.

Private Access Modifiers

Private data member and private member function of class only accessible inside the class.

To declare private data member and private member function within class, we need to use of double underscore before the attribute.

Example:

class Student:
	__rollNo = 30
	def __init__(self, name, age):
		self.studentName = name
		self.studentAge = age
	def printNow(self):
		print(f"My name is {self.studentName} and my age is {self.studentAge} and roll no is {self.__rollNo}")
				
s1 = Student('John', 34)
s1.printNow()

Output

My name is John and my age is 34 and roll no is 30

Conclusion

In this article, you have learned all about Python access modifiers along with examples. Python access modifiers are the best way to implement encapsulation. I hope you don’t have any confusion regarding access modifiers in Python.

If you like this Python access modifiers article, please share and keep visiting for further Python tutorials.

For more information:- Click Here

FAQs about Python access modifiers

What are access modifiers in Python?

Ans:- There are three types of access specifiers available in Python. Python use underscore ( _ ) to determine the access control of the attributes and methods.

What is __init__ in Python?

Ans:- __init__ in Python is a special method that calls automatically when the object of the class is created. The __init__ method in Python is also called the constructor.

What is self in Python class?

Ans:- self in Python class represents the current instance of the class. Using self, you can access properties and methods that are defined inside in class.

What do you mean by access specifiers?

Ans:- Access specifiers in Python restrict the access of attributes and methods of the class. There are three types of access specifiers available in Python.

Public access specifier:- Public access specifier accessible within and outside of the class.
Protected access specifier:- Protected access specifier accessible by class and as well as child class.
Private access specifier:- Private access specifier only accessible within the class.

Comprehension in Python
Python setattr() function

Was this post helpful?

Related Posts

Leave a Reply