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 :: python type casting 
Python :: insert-cells-in-empty-pandas-dataframe 
Python :: how to make a new column with explode pyspark 
Python :: insertion sort 
Python :: shape 
Python :: Model In View Django 
Python :: prolog finding the max from a list of facts 
Python :: pahtlib join path 
Python :: python pickle dataframe 
Python :: Python NumPy insert Function Syntax 
Python :: how to split from a specific charecter to the end of the string in python 
Python :: how to use pyttsx3 
Python :: python dataframe show row 
Python :: find type of an element in list python 
Python :: password protected cmd python 
Python :: ValueError: Graph disconnected: cannot obtain value for tensor Tensor("input_3_1:0", shape=(None, None, 71), dtype=float32) at layer "input_3". The following previous layers were accessed without issue: [] 
Python :: #index operator in python 
Python :: how to make simple login in python 
Python :: hash function in python 
Python :: django custom user model 
Python :: Python Pandas - How to write in a specific column in an Excel Sheet 
Python :: loop in python 
Python :: dockerize django 
Python :: converting list of arrays with same size to single array python 
Python :: NumPy flipud Example 
Python :: python local nosql database 
Python :: get index of first true value numpy 
Python :: how to avoid inserting duplicate records in orm django 
Python :: net way to print 2d array 
Python :: plot dataframe 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =