Encapsulation in Python
Encapsulation in Python
Definition:
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