SlideShare a Scribd company logo
Access Modifiers In
Python
SANTOSH VERMA
Public Access
 All member variables and methods are public by default in Python. So when you want to make your
member public, you just do nothing.
class Cup:
def __init__(self):
self.color = None
self.content = None
def fill(self, beverage):
self.content = beverage
def empty(self):
self.content = None
#Driver Code
redCup = Cup()
redCup.color = "red"
redCup.content = "tea"
redCup.empty()
redCup.fill("coffee")
Protected
 Protected member is (in C++ and Java) accessible only from within the class and
it’s subclasses. How to accomplish this in Python?
 The answer is – by convention. By prefixing the name of your member with a
single underscore,
class Cup:
def __init__(self):
self.color = None
self._content = None # protected variable
def fill(self, beverage):
self._content = beverage
def empty(self):
self._content = None
#Driver Code
cup = Cup()
cup._content = "tea“
print(cup.color, cup._content)
Output:
None tea
Private
 By declaring your data member private you mean, that nobody should be able
to access it from outside the class.
 Python supports a technique called name mangling. This feature turns every
member name prefixed with at least two underscores and suffixed with at
most one underscore into.
class Peak:
def _single_method(self):
pass
def __double_method(self): # for mangling
pass
class Pyth(Peak):
def __double_method(self): # for mangling
pass
Access modifiers in Python

More Related Content

What's hot

Code Optimization
Code OptimizationCode Optimization
Code Optimization
guest9f8315
 

What's hot (20)

Modular programming
Modular programmingModular programming
Modular programming
 
for loop in java
for loop in java for loop in java
for loop in java
 
File handling-c
File handling-cFile handling-c
File handling-c
 
Functions in C
Functions in CFunctions in C
Functions in C
 
Interface in java
Interface in javaInterface in java
Interface in java
 
C Programming Unit-5
C Programming Unit-5C Programming Unit-5
C Programming Unit-5
 
Functions in Python
Functions in PythonFunctions in Python
Functions in Python
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
Windowforms controls c#
Windowforms controls c#Windowforms controls c#
Windowforms controls c#
 
C by balaguruswami - e.balagurusamy
C   by balaguruswami - e.balagurusamyC   by balaguruswami - e.balagurusamy
C by balaguruswami - e.balagurusamy
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Loops c++
Loops c++Loops c++
Loops c++
 
Common dialog control
Common dialog controlCommon dialog control
Common dialog control
 
Error handling and debugging in vb
Error handling and debugging in vbError handling and debugging in vb
Error handling and debugging in vb
 
User defined functions
User defined functionsUser defined functions
User defined functions
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++
 
Programming Methodology
Programming MethodologyProgramming Methodology
Programming Methodology
 
Control structures
Control structuresControl structures
Control structures
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
File handling in c
File handling in cFile handling in c
File handling in c
 

More from Santosh Verma (9)

Migrating localhost to server
Migrating localhost to serverMigrating localhost to server
Migrating localhost to server
 
Wordpress tutorial
Wordpress tutorialWordpress tutorial
Wordpress tutorial
 
Sorting tech comparision
Sorting tech comparisionSorting tech comparision
Sorting tech comparision
 
Functions in python
Functions in pythonFunctions in python
Functions in python
 
Class, object and inheritance in python
Class, object and inheritance in pythonClass, object and inheritance in python
Class, object and inheritance in python
 
SoC: System On Chip
SoC: System On ChipSoC: System On Chip
SoC: System On Chip
 
Embedded system design using arduino
Embedded system design using arduinoEmbedded system design using arduino
Embedded system design using arduino
 
Snapdragon SoC and ARMv7 Architecture
Snapdragon SoC and ARMv7 ArchitectureSnapdragon SoC and ARMv7 Architecture
Snapdragon SoC and ARMv7 Architecture
 
Trends and innovations in Embedded System Education
Trends and innovations in Embedded System EducationTrends and innovations in Embedded System Education
Trends and innovations in Embedded System Education
 

Recently uploaded

Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
Accounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdfAccounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdf
YibeltalNibretu
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 

Recently uploaded (20)

Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
Accounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdfAccounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdf
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringBasic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
 
Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"
Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"
Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"
 
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfINU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
 
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxJose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
 
2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx
 
Forest and Wildlife Resources Class 10 Free Study Material PDF
Forest and Wildlife Resources Class 10 Free Study Material PDFForest and Wildlife Resources Class 10 Free Study Material PDF
Forest and Wildlife Resources Class 10 Free Study Material PDF
 
Gyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxGyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptx
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 

Access modifiers in Python

  • 2. Public Access  All member variables and methods are public by default in Python. So when you want to make your member public, you just do nothing. class Cup: def __init__(self): self.color = None self.content = None def fill(self, beverage): self.content = beverage def empty(self): self.content = None #Driver Code redCup = Cup() redCup.color = "red" redCup.content = "tea" redCup.empty() redCup.fill("coffee")
  • 3. Protected  Protected member is (in C++ and Java) accessible only from within the class and it’s subclasses. How to accomplish this in Python?  The answer is – by convention. By prefixing the name of your member with a single underscore, class Cup: def __init__(self): self.color = None self._content = None # protected variable def fill(self, beverage): self._content = beverage def empty(self): self._content = None #Driver Code cup = Cup() cup._content = "tea“ print(cup.color, cup._content) Output: None tea
  • 4. Private  By declaring your data member private you mean, that nobody should be able to access it from outside the class.  Python supports a technique called name mangling. This feature turns every member name prefixed with at least two underscores and suffixed with at most one underscore into. class Peak: def _single_method(self): pass def __double_method(self): # for mangling pass class Pyth(Peak): def __double_method(self): # for mangling pass