Home  >  Article  >  Backend Development  >  Access modifiers in Python: Public, Private and Protected

Access modifiers in Python: Public, Private and Protected

PHPz
PHPzforward
2023-09-02 19:17:061534browse

Access modifiers in Python: Public, Private and Protected

Access modifiers are used by object oriented programming languages ​​like C,java,python etc. to restrict the access of the class member variable and methods from outside the class. Encapsulation is an OOPs principle which protects the internal data of the class using Access modifiers like Public,Private and Protected.

Python supports three access modifiers, namely public (public), private (private) and protected (protected). These access modifiers provide restrictions on accessing the member variables and methods of a class from any object outside the class.

Public Access Modifier

By default the member variables and methods are public which means they can be accessed from anywhere outside or inside the class. No public keyword is required to make the class or methods and properties public.Here is an example of Public access modifier −

The Chinese translation of

Example

is:

Example

The student class has two member variables, name and age and a method display which prints the member variable values.Both these variables and the methods are public as no specific keyword is assigned to them.

class Student:
   def __init__(self, name, age):
      self.name = name
      self.age = age
    
   def display(self):
      print("Name:", self.name)
      print("Age:", self.age)

s = Student("John", 20)
s.display() 

Output

Name: John
Age: 20

Private access modifier

Class properties and methods with private access modifier can only be accessed within the class where they are defined and cannot be accessed outside the class. In Python private properties and methods are declared by adding a prefix with two underscores('__') before their declaration.

The Chinese translation of

Example

is:

Example

The Class BankAccount is being declared with two private variables i.e account_number and balance and a private property display_balance which prints the balance of the bank account. As both the properties and method are private so while accessing them from outside the class it raises Attribute error.

class BankAccount:
   def __init__(self, account_number, balance):
      self.__account_number = account_number
      self.__balance = balance
    
   def __display_balance(self):
      print("Balance:", self.__balance)

b = BankAccount(1234567890, 5000)
b.__display_balance() 

Output

AttributeError: 'BankAccount' object has no attribute '__display_balance'

Protected access modifier

Class properties and methods with protected access modifier can be accessed inside the class and in classes that inherit the protected class. In Python, protected members and methods are declared using a single underscore ('_') as a prefix before their name.

The Chinese translation of

Example

is:

Example

The Person class has two protected attributes, namely _name and _age, and a protected method _display, which is used to display the attribute values ​​​​of the Person class. The Student class inherits from the Person class and has an additional protected attribute _roll_number and a public method display, which calls the _display method of the parent class Person class. By creating an instance of the Student class, we can call the display method from outside the class, because the display method is private, it calls the protected _display method of the Person class.

class Person:
   def __init__(self, name, age):
      self._name = name
      self._age = age
    
   def _display(self):
      print("Name:", self._name)
      print("Age:", self._age)

class Student(Person):
   def __init__(self, name, age, roll_number):
      super().__init__(name, age)
      self._roll_number = roll_number
    
   def display(self):
      self._display()
      print("Roll Number:", self._roll_number)

s = Student("John", 20, 123)
s.display() 

Output

Name: John
Age: 20
Roll Number: 123

in conclusion

In this article, we learned about three access modifiers used for data hiding and protection in Python and other object-oriented programming languages. Public, private and protected are the three access modifiers used in Python. The public properties and methods of a class can be accessed from anywhere inside or outside the class. Private member variables and properties can only be accessed from within the class in which they are declared. Protected access modifier is used when we need to access the properties and methods of a class from within the class and from classes that inherit from that class.

The above is the detailed content of Access modifiers in Python: Public, Private and Protected. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete