0% found this document useful (1 vote)
40 views

Encapsulation in Python

Uploaded by

pugazhm125
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
40 views

Encapsulation in Python

Uploaded by

pugazhm125
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Encaptulation in Python

Definition:

 Encaptulation is the process of hiding the internal state and functionality of


an object and only exposing a controlled interface to the outside world.
 This is done to ensure that the data is not directly accessible and can only be
modified in a controlled manner.
 It also involves restricting direct access to some of the object's components,
which is a means of preventing accidental interference and misuse of the
data.
Access Modifiers

1). Private:
 The private access modifier means that the members (variables and methods)
can only be accessed within the same class.
 They are not accessible from outside the class or by any other class.
Example:
class Person:
def __init__(self, name):
self.__name = name # private variable
2). Public:
 Public members are accessible from anywhere. By default, all members of a
class are public unless specified otherwise.

Example:
class Person:
def __init__(self, name):
self.name = name # public variable
3). Protected:
 Indicated by a single underscore (_) and should not be accessed directly
outside the class. However, this is just a convention and does not prevent
access.

Example:
class Person:
def __init__(self, name):
self._name = name # protected variable

You might also like