Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

encapsulation in python

# convention: _<name> for protected and __<name> for private
class MyClass: 
    def __init__(self): 
          
        # Protected
        # No access outside of the class or subclasses
        self._this_is_protected = True
        # Private 
        # No access outside of the class
        self.__this_is_private = True

# Note:
# Private and protected members can be accessed outside of the class using python name mangling.
Comment

program to demonstrate encapsulation in python

# Python program to
# demonstrate protected members
 
# Creating a base class
class Base:
    def __init__(self):
 
        # Protected member
        self._a = 2
 
# Creating a derived class
class Derived(Base):
    def __init__(self):
 
        # Calling constructor of
        # Base class
        Base.__init__(self)
        print("Calling protected member of base class: ",
              self._a)
 
        # Modify the protected variable:
        self._a = 3
        print("Calling modified protected member outside class: ",
              self._a)
 
 
obj1 = Derived()
 
obj2 = Base()
 
# Calling protected member
# Can be accessed but should not be done due to convention
print("Accessing protedted member of obj1: ", obj1._a)
 
# Accessing the protected variable outside
print("Accessing protedted member of obj2: ", obj2._a)
Comment

program to demonstrate encapsulation in python

# Python program to
# demonstrate private members
 
# Creating a Base class
 
 
class Base:
    def __init__(self):
        self.a = "GeeksforGeeks"
        self.__c = "GeeksforGeeks"
 
# Creating a derived class
class Derived(Base):
    def __init__(self):
 
        # Calling constructor of
        # Base class
        Base.__init__(self)
        print("Calling private member of base class: ")
        print(self.__c)
 
 
# Driver code
obj1 = Base()
print(obj1.a)
 
# Uncommenting print(obj1.c) will
# raise an AttributeError
 
# Uncommenting obj2 = Derived() will
# also raise an AtrributeError as
# private member of base class
# is called inside derived class
Comment

Data Encapsulation in Python

class Computer:
   def __init__(self):
        self.__maxprice = 900
    def sell(self):
        print("Selling Price: {}".format(self.__maxprice))
    def setMaxPrice(self, price):
        self.__maxprice = price

c = Computer()
c.sell()

# change the price
c.__maxprice = 1000
c.sell()

# using setter function
c.setMaxPrice(1000)
c.sell()
Comment

PREVIOUS NEXT
Code Example
Python :: reshape (-1,1) 
Python :: python replace text 
Python :: python inspect 
Python :: flask get with parameters 
Python :: how to get the end of a item in a python array 
Python :: Finding if 2 consecutive numbers in a list have a sum equal to a given number 
Python :: pandas split cell into multiple columns 
Python :: class in python 
Python :: add values to dictionary key python 
Python :: PySimple list of elements 
Python :: Python Program to Shuffle Deck of Cards 
Python :: how to delete in python 
Python :: run python test in terminal 
Python :: np random list 
Python :: como poner estado a un bot en discord 
Python :: hwo to except every error in python try statemen 
Python :: select column in pandas dataframe 
Python :: how to insert values to database with using dictionary in python 
Python :: extract decimal number from string python 
Python :: python cursor placement 
Python :: string in python 
Python :: split function python 
Python :: new paragraph python 
Python :: python check if false in dict 
Python :: percent sign in python 
Python :: python use variable name as string 
Python :: pandas replace word begins with contains 
Python :: Python Tkinter MenuButton Widget 
Python :: chat application in python 
Python :: python pass arguments in command line 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =