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

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 3 
Python :: Python NumPy ndarray flatten Function Syntax 
Python :: python size of set 
Python :: KeyError 
Python :: Multiple list comprehension 
Python :: convert images to jpeg 
Python :: print variable python 
Python :: subarrays in python 
Python :: dfs 
Python :: python incrémentation 
Python :: python convert time 
Python :: doing some math in python 
Python :: pandas idxmax 
Python :: tuplein python 
Python :: pandas drop rows 
Python :: minmax python 
Python :: how to make loop python 
Python :: python move files 
Python :: get element by index in list python 
Python :: get number of row dataframe pandas 
Python :: subtract constant from list 
Python :: python append many items to a list 
Python :: convert from R to python 
Python :: python function pointer with multiple args 
Python :: pandas cummax 
Python :: rscript convert r to python script 
Python :: how to deploy to shinyapps.io 
Python :: list devices python 3 
Python :: flask pass list to another view 
Python :: pyglet template 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =