Access modifiers in Python: Public, Private and Protected

PHPz
Release: 2023-09-02 19:17:06
forward
1576 people have browsed it

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() 
Copy after login

Output

Name: John
Age: 20
Copy after login

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() 
Copy after login

Output

AttributeError: 'BankAccount' object has no attribute '__display_balance'
Copy after login

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() 
Copy after login

Output

Name: John
Age: 20
Roll Number: 123
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!