Home  >  Article  >  Backend Development  >  What is encapsulated in Python?

What is encapsulated in Python?

PHPz
PHPzforward
2023-09-02 15:21:061573browse

What is encapsulated in Python?

Encapsulation is one of the key concepts of object-oriented languages ​​such as Python and Java. Encapsulation is used to restrict access to methods and variables. In encapsulation, code and data are wrapped in a unit and protected from accidental modification.

Encapsulation is a mechanism that packages data (variables) and code that acts on the data (methods) together as a unit. In encapsulation, variables of one class are hidden from other classes and can only be accessed through methods of the current class.

Encapsulation example

Suppose we have a company selling courses to students, engineers, and professionals. The different departments of the company include operations, finance, accounting, sales, etc. Now, if an employee in the accounting department needs sales records for 2022, he/she does not have direct access.

To access, Customer Department employees need permission from a Sales Department team member. Therefore, sales data is hidden from other departments, similarly, the company's financial data can only be accessed by Financial Data and is hidden from other departments. Account, sales, finance, operations, marketing and other data are hidden from other parts

Use classes to implement encapsulation in Python

Another example of encapsulation is a class, because a class combines data and methods into a single unit. Here, the custom function demofunc() displays the student’s record where we can access the public data members. Using objects st1, st2, st3, st4, we access the public methods of the class demofunc() -

Example

class Students:
   def __init__(self, name, rank, points):
      self.name = name
      self.rank = rank
      self.points = points

   # custom function
   def demofunc(self):
      print("I am "+self.name)
      print("I got Rank ",+self.rank)

# create 4 objects
st1 = Students("Steve", 1, 100)
st2 = Students("Chris", 2, 90)
st3 = Students("Mark", 3, 76)
st4 = Students("Kate", 4, 60)

# call the functions using the objects created above
st1.demofunc()
st2.demofunc()
st3.demofunc()
st4.demofunc()

Output

I am Steve
I got Rank  1
I am Chris
I got Rank  2
I am Mark
I got Rank  3
I am Kate
I got Rank  4

Python access modifiers

Let’s look at access modifiers in Python to understand the concepts of encapsulation and data hiding

  • public
  • private
  • protected

Public access modifier

Public members can be accessed from inside or outside the class.

Private access modifier

Private members can only be accessed within the class. Define private members by adding two underscores before the member name, for example

__age

Protected access modifier

Protected members can access. From within a class and its subclasses. Define protected members by prefixing the member name with an underscore, for example

_points

The above is the detailed content of What is encapsulated in Python?. 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