Access Modifiers in Python

  • December 13, 2022
  • python
JOIN Clause
  • When implementing the concepts of inheritance in Python code, access specifiers or access modifiers are used to restrict the access of class variables and methods outside the class.
  • The three access modifiers most programming languages use are Public, Protected, and Private in a class.
  • The access control for a particular data member or member function of a class is determined by Python using the “_” symbol. To protect information from being misused and unauthorized access, access specifiers in Python are crucial.
Public Members:

Accessible from outside the class are public members, which are typically methods declared in the class. An object belonging to the same class must be used to call a public method. The concept of data encapsulation is guaranteed by the combination of private instance variables and public methods.

A Python class’s public members are all by default. From outside the class environment, anyone can access any member.

Example:

# program to illustrate public access modifier in a class

class Student:
	
	# constructor
	def __init__(self, name, roll):
		
		# public data members
		self.Name = name
		self.Roll = roll

	# public member function	
	def display(self):
		
		# accessing public data member
		print("Roll: ", self.Roll)

# creating object of the class
obj = Student("Mayank Sharma", 31)

# accessing public data member
print("Name: ", obj.Name)

# calling public member function of the class
obj.display()

Output:

Name:  Mayank Sharma
Roll:  31
Protected Members:

A class’s protected members can be accessed from within the class and by its subclasses as well. Access to any other environment is not allowed. This makes it possible for the child class to inherit particular parent class resources.

To make an instance variable protected in Python, prefix it with (a single underscore). This effectively prevents access to it unless it comes from a subclass.

Example:


class Student:
	
	# protected data members
	_name = None
	_roll = None
	
	# constructor
	def __init__(self, name, roll):
		self._name = name
		self._roll = roll
	
	# protected member function
	def _displayRoll(self):

		# accessing protected data members
		print("Roll: ", self._roll)

# derived class
class child(Student):

	# constructor
	def __init__(self, name, roll):
				Student.__init__(self, name, roll)
		
	# public member function
	def displayDetails(self):
				
				# accessing protected data members of super class
				print("Name: ", self._name)
				
				# accessing protected member functions of super class
				self._displayRoll()

# creating objects of the derived class	
obj = child("Mayank Sharma", 1706256)

# calling public member functions of the class
obj.displayDetails()

Output:

Name:  Mayank Sharma
Roll:  1706256
Private Members:

The most secure access modifier is private, which restricts access to members of a class that have been declared private to other members of the same class only. By placing a double underscore ‘__’ symbol before a class’s data member, the data member can be made private.It gives a strong suggestion not to touch it from outside the class. Any attempt to do so will result in an Attribute Error.

Example:


class Student:
	
	# private members
	__name = None
	__roll = None
	# constructor
	def __init__(self, name, roll):
		self.__name = name
		self.__roll = roll

	# private member function
	def __display(self):
		
		# accessing private data members
		print("Name: ", self.__name)
		print("Roll: ", self.__roll)
	
	# public member function
	def accessPrivateFunction(self):
			
		# accessing private member function
		self.__display()

# creating object
obj = Student("Mayank Sharma", 1706256)

# calling public member function of the class
obj.accessPrivateFunction()

Output:

Name:  Mayank Sharma
Roll:  1706256

Note: also read about Accessing Elements of 2-D Array

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Leave a Reply

Your email address will not be published. Required fields are marked *